From nobody Sun Nov 24 09:39:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17259719039751016.137716527795; Tue, 10 Sep 2024 05:38:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1so07c-0006b8-Qb; Tue, 10 Sep 2024 08:37:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1so06k-0002IP-Mg for qemu-devel@nongnu.org; Tue, 10 Sep 2024 08:36:35 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1so06h-00053B-Ig for qemu-devel@nongnu.org; Tue, 10 Sep 2024 08:36:34 -0400 Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Ax5+hGPeBmbLkDAA--.7661S3; Tue, 10 Sep 2024 20:36:22 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by front2 (Coremail) with SMTP id qciowMBx+cVCPeBmXGoDAA--.15753S4; Tue, 10 Sep 2024 20:36:21 +0800 (CST) From: Xianglai Li To: qemu-devel@nongnu.org Cc: Tianrui Zhao , Paolo Bonzini , Song Gao , Jiaxun Yang , Huacai Chen , "Michael S. Tsirkin" , Cornelia Huck , kvm@vger.kernel.org, Bibo Mao Subject: [RFC PATCH V2 2/5] hw/loongarch: Add KVM IPI device support Date: Tue, 10 Sep 2024 20:18:29 +0800 Message-Id: X-Mailer: git-send-email 2.39.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qciowMBx+cVCPeBmXGoDAA--.15753S4 X-CM-SenderInfo: 5ol0xt5qjotxo6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=114.242.206.163; envelope-from=lixianglai@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1725971904550116600 Content-Type: text/plain; charset="utf-8" Added ipi interrupt controller for kvm emulation. The main process is to send the command word for creating an ipi device to the kernel. When the VM is saved, the ioctl obtains the ipi interrupt controller data in the kernel and saves it. When the VM is recovered, the saved data is sent to the kernel. Signed-off-by: Tianrui Zhao Signed-off-by: Xianglai Li --- Cc: Paolo Bonzini Cc: Song Gao Cc: Jiaxun Yang Cc: Huacai Chen Cc: "Michael S. Tsirkin" Cc: Cornelia Huck Cc: kvm@vger.kernel.org Cc: Bibo Mao Cc: Xianglai Li hw/intc/Kconfig | 3 + hw/intc/loongarch_ipi_kvm.c | 128 ++++++++++++++++++++++++++++++++++ hw/intc/loongson_ipi_common.c | 28 ++++++++ hw/intc/meson.build | 1 + hw/loongarch/Kconfig | 1 + hw/loongarch/virt.c | 40 +++++++---- target/loongarch/kvm/kvm.c | 1 + 7 files changed, 189 insertions(+), 13 deletions(-) create mode 100644 hw/intc/loongarch_ipi_kvm.c diff --git a/hw/intc/Kconfig b/hw/intc/Kconfig index dd405bdb5d..5201505f23 100644 --- a/hw/intc/Kconfig +++ b/hw/intc/Kconfig @@ -98,6 +98,9 @@ config LOONGARCH_IPI bool select LOONGSON_IPI_COMMON =20 +config LOONGARCH_IPI_KVM + bool + config LOONGARCH_PCH_PIC bool select UNIMP diff --git a/hw/intc/loongarch_ipi_kvm.c b/hw/intc/loongarch_ipi_kvm.c new file mode 100644 index 0000000000..74ed83d89f --- /dev/null +++ b/hw/intc/loongarch_ipi_kvm.c @@ -0,0 +1,128 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * LoongArch IPI interrupt support + * + * Copyright (C) 2024 Loongson Technology Corporation Limited + */ + +#include "qemu/osdep.h" +#include "hw/boards.h" +#include "sysemu/kvm.h" +#include "qapi/error.h" +#include "hw/intc/loongarch_ipi.h" +#include "target/loongarch/cpu.h" + +static AddressSpace *get_iocsr_as(CPUState *cpu) +{ + return LOONGARCH_CPU(cpu)->env.address_space_iocsr; +} + +static void kvm_ipi_access_regs(int fd, uint64_t addr, + uint32_t *val, bool is_write) +{ + kvm_device_access(fd, KVM_DEV_LOONGARCH_IPI_GRP_REGS, + addr, val, is_write, &error_abort); +} +static void kvm_loongarch_ipi_save_load_regs(void *opaque, bool is_write) +{ + LoongsonIPICommonState *ipi =3D (LoongsonIPICommonState *)opaque; + KVMLoongarchIPIState *s =3D KVM_LOONGARCH_IPI(opaque); + IPICore *cpu; + uint64_t attr; + int cpu_id =3D 0; + int fd =3D s->dev_fd; + + for (cpu_id =3D 0; cpu_id < ipi->num_cpu; cpu_id++) { + cpu =3D &ipi->cpu[cpu_id]; + attr =3D (cpu_id << 16) | CORE_STATUS_OFF; + kvm_ipi_access_regs(fd, attr, &cpu->status, is_write); + + attr =3D (cpu_id << 16) | CORE_EN_OFF; + kvm_ipi_access_regs(fd, attr, &cpu->en, is_write); + + attr =3D (cpu_id << 16) | CORE_SET_OFF; + kvm_ipi_access_regs(fd, attr, &cpu->set, is_write); + + attr =3D (cpu_id << 16) | CORE_CLEAR_OFF; + kvm_ipi_access_regs(fd, attr, &cpu->clear, is_write); + + attr =3D (cpu_id << 16) | CORE_BUF_20; + kvm_ipi_access_regs(fd, attr, &cpu->buf[0], is_write); + + attr =3D (cpu_id << 16) | CORE_BUF_28; + kvm_ipi_access_regs(fd, attr, &cpu->buf[2], is_write); + + attr =3D (cpu_id << 16) | CORE_BUF_30; + kvm_ipi_access_regs(fd, attr, &cpu->buf[4], is_write); + + attr =3D (cpu_id << 16) | CORE_BUF_38; + kvm_ipi_access_regs(fd, attr, &cpu->buf[6], is_write); + } +} + +static void kvm_loongarch_ipi_pre_save(LoongsonIPICommonState *opaque) +{ + kvm_loongarch_ipi_save_load_regs(opaque, false); +} + +static void kvm_loongarch_ipi_post_load(LoongsonIPICommonState *opaque, + int version_id) +{ + kvm_loongarch_ipi_save_load_regs(opaque, true); +} + +static void kvm_loongarch_ipi_realize(DeviceState *dev, Error **errp) +{ + KVMLoongarchIPIState *s =3D KVM_LOONGARCH_IPI(dev); + KVMLoongArchIPIClass *klic =3D KVM_LOONGARCH_IPI_GET_CLASS(dev); + struct kvm_create_device cd =3D {0}; + Error *local_err =3D NULL; + int ret; + + klic->parent_realize(dev, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + + cd.type =3D KVM_DEV_TYPE_LOONGARCH_IPI; + ret =3D kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd); + if (ret < 0) { + error_setg_errno(errp, errno, "Creating the KVM device failed"); + return; + } + s->dev_fd =3D cd.fd; +} + +static void kvm_loongarch_ipi_unrealize(DeviceState *dev) +{ + KVMLoongArchIPIClass *klic =3D KVM_LOONGARCH_IPI_GET_CLASS(dev); + klic->parent_unrealize(dev); +} + +static void kvm_loongarch_ipi_class_init(ObjectClass *klass, void *data) +{ + LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_CLASS(klass); + KVMLoongArchIPIClass *klic =3D KVM_LOONGARCH_IPI_CLASS(klass); + DeviceClass *dc =3D DEVICE_CLASS(klass); + + device_class_set_parent_realize(dc, kvm_loongarch_ipi_realize, + &klic->parent_realize); + device_class_set_parent_unrealize(dc, kvm_loongarch_ipi_unrealize, + &klic->parent_unrealize); + + licc->get_iocsr_as =3D get_iocsr_as; + licc->cpu_by_arch_id =3D cpu_by_arch_id; + licc->pre_save =3D kvm_loongarch_ipi_pre_save; + licc->post_load =3D kvm_loongarch_ipi_post_load; +} + +static const TypeInfo kvm_loongarch_ipi_types[] =3D { + { + .name =3D TYPE_KVM_LOONGARCH_IPI, + .parent =3D TYPE_LOONGSON_IPI_COMMON, + .class_init =3D kvm_loongarch_ipi_class_init, + } +}; + +DEFINE_TYPES(kvm_loongarch_ipi_types) diff --git a/hw/intc/loongson_ipi_common.c b/hw/intc/loongson_ipi_common.c index a6ce0181f6..8b0683bd40 100644 --- a/hw/intc/loongson_ipi_common.c +++ b/hw/intc/loongson_ipi_common.c @@ -289,10 +289,38 @@ static void loongson_ipi_common_unrealize(DeviceState= *dev) g_free(s->cpu); } =20 +static int loongson_ipi_pre_save(void *opaque) +{ + IPICore *ipicore =3D (IPICore *)opaque; + LoongsonIPICommonState *s =3D ipicore->ipi; + LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(s); + + if (licc->pre_save) { + licc->pre_save(s); + } + + return 0; +} + +static int loongson_ipi_post_load(void *opaque, int version_id) +{ + IPICore *ipicore =3D (IPICore *)opaque; + LoongsonIPICommonState *s =3D ipicore->ipi; + LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(s); + + if (licc->post_load) { + licc->post_load(s, version_id); + } + + return 0; +} + static const VMStateDescription vmstate_ipi_core =3D { .name =3D "ipi-single", .version_id =3D 2, .minimum_version_id =3D 2, + .pre_save =3D loongson_ipi_pre_save, + .post_load =3D loongson_ipi_post_load, .fields =3D (const VMStateField[]) { VMSTATE_UINT32(status, IPICore), VMSTATE_UINT32(en, IPICore), diff --git a/hw/intc/meson.build b/hw/intc/meson.build index f4d81eb8e4..f55eb1b80b 100644 --- a/hw/intc/meson.build +++ b/hw/intc/meson.build @@ -72,6 +72,7 @@ specific_ss.add(when: 'CONFIG_M68K_IRQC', if_true: files(= 'm68k_irqc.c')) specific_ss.add(when: 'CONFIG_LOONGSON_IPI_COMMON', if_true: files('loongs= on_ipi_common.c')) specific_ss.add(when: 'CONFIG_LOONGSON_IPI', if_true: files('loongson_ipi.= c')) specific_ss.add(when: 'CONFIG_LOONGARCH_IPI', if_true: files('loongarch_ip= i.c')) +specific_ss.add(when: 'CONFIG_LOONGARCH_IPI_KVM', if_true: files('loongarc= h_ipi_kvm.c')) specific_ss.add(when: 'CONFIG_LOONGARCH_PCH_PIC', if_true: files('loongarc= h_pch_pic.c')) specific_ss.add(when: 'CONFIG_LOONGARCH_PCH_MSI', if_true: files('loongarc= h_pch_msi.c')) specific_ss.add(when: 'CONFIG_LOONGARCH_EXTIOI', if_true: files('loongarch= _extioi.c')) diff --git a/hw/loongarch/Kconfig b/hw/loongarch/Kconfig index 0de713a439..f8fcac3e7b 100644 --- a/hw/loongarch/Kconfig +++ b/hw/loongarch/Kconfig @@ -16,6 +16,7 @@ config LOONGARCH_VIRT select LOONGARCH_PCH_PIC select LOONGARCH_PCH_MSI select LOONGARCH_EXTIOI + select LOONGARCH_IPI_KVM if KVM select LS7A_RTC select SMBIOS select ACPI_PCI diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c index 29040422aa..3b28e8e671 100644 --- a/hw/loongarch/virt.c +++ b/hw/loongarch/virt.c @@ -48,6 +48,7 @@ #include "hw/block/flash.h" #include "hw/virtio/virtio-iommu.h" #include "qemu/error-report.h" +#include "sysemu/kvm.h" =20 static bool virt_is_veiointc_enabled(LoongArchVirtMachineState *lvms) { @@ -788,15 +789,32 @@ static void virt_irq_init(LoongArchVirtMachineState *= lvms) */ =20 /* Create IPI device */ - ipi =3D qdev_new(TYPE_LOONGARCH_IPI); - qdev_prop_set_uint32(ipi, "num-cpu", ms->smp.cpus); - sysbus_realize_and_unref(SYS_BUS_DEVICE(ipi), &error_fatal); - - /* IPI iocsr memory region */ - memory_region_add_subregion(&lvms->system_iocsr, SMP_IPI_MAILBOX, - sysbus_mmio_get_region(SYS_BUS_DEVICE(ipi), 0)); - memory_region_add_subregion(&lvms->system_iocsr, MAIL_SEND_ADDR, - sysbus_mmio_get_region(SYS_BUS_DEVICE(ipi), 1)); + if (kvm_enabled() && kvm_irqchip_in_kernel()) { + ipi =3D qdev_new(TYPE_KVM_LOONGARCH_IPI); + qdev_prop_set_int32(ipi, "num-cpu", ms->smp.cpus); + sysbus_realize_and_unref(SYS_BUS_DEVICE(ipi), &error_fatal); + } else { + ipi =3D qdev_new(TYPE_LOONGARCH_IPI); + qdev_prop_set_uint32(ipi, "num-cpu", ms->smp.cpus); + sysbus_realize_and_unref(SYS_BUS_DEVICE(ipi), &error_fatal); + + /* IPI iocsr memory region */ + memory_region_add_subregion(&lvms->system_iocsr, SMP_IPI_MAILBOX, + sysbus_mmio_get_region(SYS_BUS_DEVICE(ipi), 0)); + memory_region_add_subregion(&lvms->system_iocsr, MAIL_SEND_ADDR, + sysbus_mmio_get_region(SYS_BUS_DEVICE(ipi), 1)); + + for (cpu =3D 0; cpu < ms->smp.cpus; cpu++) { + cpu_state =3D qemu_get_cpu(cpu); + cpudev =3D DEVICE(cpu_state); + lacpu =3D LOONGARCH_CPU(cpu_state); + env =3D &(lacpu->env); + + /* connect ipi irq to cpu irq */ + qdev_connect_gpio_out(ipi, cpu, qdev_get_gpio_in(cpudev, IRQ_I= PI)); + env->ipistate =3D ipi; + } + } =20 /* Add cpu interrupt-controller */ fdt_add_cpuic_node(lvms, &cpuintc_phandle); @@ -807,10 +825,6 @@ static void virt_irq_init(LoongArchVirtMachineState *l= vms) lacpu =3D LOONGARCH_CPU(cpu_state); env =3D &(lacpu->env); env->address_space_iocsr =3D &lvms->as_iocsr; - - /* connect ipi irq to cpu irq */ - qdev_connect_gpio_out(ipi, cpu, qdev_get_gpio_in(cpudev, IRQ_IPI)); - env->ipistate =3D ipi; } =20 /* Create EXTIOI device */ diff --git a/target/loongarch/kvm/kvm.c b/target/loongarch/kvm/kvm.c index e1be6a6959..c07dcfd85f 100644 --- a/target/loongarch/kvm/kvm.c +++ b/target/loongarch/kvm/kvm.c @@ -719,6 +719,7 @@ int kvm_arch_get_default_type(MachineState *ms) =20 int kvm_arch_init(MachineState *ms, KVMState *s) { + s->kernel_irqchip_allowed =3D false; cap_has_mp_state =3D kvm_check_extension(s, KVM_CAP_MP_STATE); return 0; } --=20 2.39.1