From nobody Sun Nov 24 08:23:34 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 1725972151628873.8283599627051; Tue, 10 Sep 2024 05:42:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1so07R-0005P8-75; Tue, 10 Sep 2024 08:37:19 -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-0002J4-SX 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-00052Z-Q4 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 _____8CxhehHPeBmcrkDAA--.7713S3; Tue, 10 Sep 2024 20:36:23 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by front2 (Coremail) with SMTP id qciowMBx+cVCPeBmXGoDAA--.15753S5; Tue, 10 Sep 2024 20:36:22 +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 3/5] hw/loongarch: Add KVM extioi device support Date: Tue, 10 Sep 2024 20:18:30 +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--.15753S5 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: 1725972154169116600 Content-Type: text/plain; charset="utf-8" Added extioi interrupt controller for kvm emulation. The main process is to send the command word for creating an extioi device to the kernel. When the VM is saved, the ioctl obtains the related data of the extioi interrupt controller 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_extioi_kvm.c | 250 +++++++++++++++++++++++++++++++++ hw/intc/meson.build | 1 + hw/loongarch/Kconfig | 1 + hw/loongarch/virt.c | 51 ++++--- 5 files changed, 285 insertions(+), 21 deletions(-) create mode 100644 hw/intc/loongarch_extioi_kvm.c diff --git a/hw/intc/Kconfig b/hw/intc/Kconfig index 5201505f23..df9352d41d 100644 --- a/hw/intc/Kconfig +++ b/hw/intc/Kconfig @@ -112,3 +112,6 @@ config LOONGARCH_PCH_MSI =20 config LOONGARCH_EXTIOI bool + +config LOONGARCH_EXTIOI_KVM + bool diff --git a/hw/intc/loongarch_extioi_kvm.c b/hw/intc/loongarch_extioi_kvm.c new file mode 100644 index 0000000000..139a00ac2a --- /dev/null +++ b/hw/intc/loongarch_extioi_kvm.c @@ -0,0 +1,250 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * LoongArch kvm extioi interrupt support + * + * Copyright (C) 2024 Loongson Technology Corporation Limited + */ + +#include "qemu/osdep.h" +#include "hw/qdev-properties.h" +#include "qemu/typedefs.h" +#include "hw/intc/loongarch_extioi.h" +#include "hw/sysbus.h" +#include "linux/kvm.h" +#include "migration/vmstate.h" +#include "qapi/error.h" +#include "sysemu/kvm.h" + +static void kvm_extioi_access_regs(int fd, uint64_t addr, + void *val, bool is_write) +{ + kvm_device_access(fd, KVM_DEV_LOONGARCH_EXTIOI_GRP_REGS, + addr, val, is_write, &error_abort); +} + +static void kvm_extioi_access_sw_status(int fd, uint64_t addr, + void *val, bool is_write) +{ + kvm_device_access(fd, KVM_DEV_LOONGARCH_EXTIOI_GRP_SW_STATUS, + addr, val, is_write, &error_abort); +} + +static void kvm_extioi_save_load_sw_status(void *opaque, bool is_write) +{ + KVMLoongArchExtIOI *s =3D (KVMLoongArchExtIOI *)opaque; + KVMLoongArchExtIOIClass *class =3D KVM_LOONGARCH_EXTIOI_GET_CLASS(s); + int fd =3D class->dev_fd; + int addr; + + addr =3D KVM_DEV_LOONGARCH_EXTIOI_SW_STATUS_NUM_CPU; + kvm_extioi_access_sw_status(fd, addr, (void *)&s->num_cpu, is_write); + + addr =3D KVM_DEV_LOONGARCH_EXTIOI_SW_STATUS_FEATURE; + kvm_extioi_access_sw_status(fd, addr, (void *)&s->features, is_write); + + addr =3D KVM_DEV_LOONGARCH_EXTIOI_SW_STATUS_STATE; + kvm_extioi_access_sw_status(fd, addr, (void *)&s->status, is_write); +} + +static void kvm_extioi_save_load_regs(void *opaque, bool is_write) +{ + KVMLoongArchExtIOI *s =3D (KVMLoongArchExtIOI *)opaque; + KVMLoongArchExtIOIClass *class =3D KVM_LOONGARCH_EXTIOI_GET_CLASS(s); + int fd =3D class->dev_fd; + int addr, offset, cpuid; + + for (addr =3D EXTIOI_NODETYPE_START; addr < EXTIOI_NODETYPE_END; addr = +=3D 4) { + offset =3D (addr - EXTIOI_NODETYPE_START) / 4; + kvm_extioi_access_regs(fd, addr, + (void *)&s->nodetype[offset], is_write); + } + + for (addr =3D EXTIOI_IPMAP_START; addr < EXTIOI_IPMAP_END; addr +=3D 4= ) { + offset =3D (addr - EXTIOI_IPMAP_START) / 4; + kvm_extioi_access_regs(fd, addr, (void *)&s->ipmap[offset], is_wri= te); + } + + for (addr =3D EXTIOI_ENABLE_START; addr < EXTIOI_ENABLE_END; addr +=3D= 4) { + offset =3D (addr - EXTIOI_ENABLE_START) / 4; + kvm_extioi_access_regs(fd, addr, + (void *)&s->enable[offset], is_write); + } + + for (addr =3D EXTIOI_BOUNCE_START; addr < EXTIOI_BOUNCE_END; addr +=3D= 4) { + offset =3D (addr - EXTIOI_BOUNCE_START) / 4; + kvm_extioi_access_regs(fd, addr, + (void *)&s->bounce[offset], is_write); + } + + for (addr =3D EXTIOI_ISR_START; addr < EXTIOI_ISR_END; addr +=3D 4) { + offset =3D (addr - EXTIOI_ISR_START) / 4; + kvm_extioi_access_regs(fd, addr, + (void *)&s->isr[offset], is_write); + } + + for (addr =3D EXTIOI_COREMAP_START; addr < EXTIOI_COREMAP_END; addr += =3D 4) { + offset =3D (addr - EXTIOI_COREMAP_START) / 4; + kvm_extioi_access_regs(fd, addr, + (void *)&s->coremap[offset], is_write); + } + + for (cpuid =3D 0; cpuid < s->num_cpu; cpuid++) { + for (addr =3D EXTIOI_COREISR_START; + addr < EXTIOI_COREISR_END; addr +=3D 4) { + offset =3D (addr - EXTIOI_COREISR_START) / 4; + addr =3D (cpuid << 16) | addr; + kvm_extioi_access_regs(fd, addr, + (void *)&s->coreisr[cpuid][offset], is_write= ); + } + } +} + +static int kvm_loongarch_extioi_pre_save(void *opaque) +{ + kvm_extioi_save_load_regs(opaque, false); + kvm_extioi_save_load_sw_status(opaque, false); + return 0; +} + +static int kvm_loongarch_extioi_post_load(void *opaque, int version_id) +{ + KVMLoongArchExtIOI *s =3D (KVMLoongArchExtIOI *)opaque; + KVMLoongArchExtIOIClass *class =3D KVM_LOONGARCH_EXTIOI_GET_CLASS(s); + int fd =3D class->dev_fd; + + kvm_extioi_save_load_regs(opaque, true); + kvm_extioi_save_load_sw_status(opaque, true); + + kvm_device_access(fd, KVM_DEV_LOONGARCH_EXTIOI_GRP_CTRL, + KVM_DEV_LOONGARCH_EXTIOI_CTRL_LOAD_FINISHED, + NULL, true, &error_abort); + return 0; +} + +static void kvm_loongarch_extioi_realize(DeviceState *dev, Error **errp) +{ + KVMLoongArchExtIOIClass *extioi_class =3D KVM_LOONGARCH_EXTIOI_GET_CLA= SS(dev); + KVMLoongArchExtIOI *s =3D KVM_LOONGARCH_EXTIOI(dev); + struct kvm_create_device cd =3D {0}; + Error *err =3D NULL; + int ret, i; + + extioi_class->parent_realize(dev, &err); + if (err) { + error_propagate(errp, err); + return; + } + + if (s->num_cpu =3D=3D 0) { + error_setg(errp, "num-cpu must be at least 1"); + return; + } + + + if (extioi_class->is_created) { + error_setg(errp, "extioi had be created"); + return; + } + + if (s->features & BIT(EXTIOI_HAS_VIRT_EXTENSION)) { + s->features |=3D EXTIOI_VIRT_HAS_FEATURES; + } + + cd.type =3D KVM_DEV_TYPE_LOONGARCH_EXTIOI; + ret =3D kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd); + if (ret < 0) { + error_setg_errno(errp, errno, + "Creating the KVM extioi device failed"); + return; + } + extioi_class->is_created =3D true; + extioi_class->dev_fd =3D cd.fd; + + ret =3D kvm_device_access(cd.fd, KVM_DEV_LOONGARCH_EXTIOI_GRP_CTRL, + KVM_DEV_LOONGARCH_EXTIOI_CTRL_INIT_NUM_CPU, + &s->num_cpu, true, NULL); + if (ret < 0) { + error_setg_errno(errp, errno, + "KVM EXTIOI: failed to set the num-cpu of EXTIOI"= ); + exit(1); + } + + ret =3D kvm_device_access(cd.fd, KVM_DEV_LOONGARCH_EXTIOI_GRP_CTRL, + KVM_DEV_LOONGARCH_EXTIOI_CTRL_INIT_FEATURE, + &s->features, true, NULL); + if (ret < 0) { + error_setg_errno(errp, errno, + "KVM EXTIOI: failed to set the feature of EXTIOI"= ); + exit(1); + } + + fprintf(stdout, "Create LoongArch extioi irqchip in KVM done!\n"); + + kvm_async_interrupts_allowed =3D true; + kvm_msi_via_irqfd_allowed =3D kvm_irqfds_enabled(); + if (kvm_has_gsi_routing()) { + for (i =3D 0; i < 64; ++i) { + kvm_irqchip_add_irq_route(kvm_state, i, 0, i); + } + kvm_gsi_routing_allowed =3D true; + } +} + +static const VMStateDescription vmstate_kvm_extioi_core =3D { + .name =3D "kvm-extioi-single", + .version_id =3D 1, + .minimum_version_id =3D 1, + .pre_save =3D kvm_loongarch_extioi_pre_save, + .post_load =3D kvm_loongarch_extioi_post_load, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32_ARRAY(nodetype, KVMLoongArchExtIOI, + EXTIOI_IRQS_NODETYPE_COUNT / 2), + VMSTATE_UINT32_ARRAY(bounce, KVMLoongArchExtIOI, + EXTIOI_IRQS_GROUP_COUNT), + VMSTATE_UINT32_ARRAY(isr, KVMLoongArchExtIOI, EXTIOI_IRQS / 32), + VMSTATE_UINT32_2DARRAY(coreisr, KVMLoongArchExtIOI, EXTIOI_CPUS, + EXTIOI_IRQS_GROUP_COUNT), + VMSTATE_UINT32_ARRAY(enable, KVMLoongArchExtIOI, EXTIOI_IRQS / 32), + VMSTATE_UINT32_ARRAY(ipmap, KVMLoongArchExtIOI, + EXTIOI_IRQS_IPMAP_SIZE / 4), + VMSTATE_UINT32_ARRAY(coremap, KVMLoongArchExtIOI, EXTIOI_IRQS / 4), + VMSTATE_UINT8_ARRAY(sw_coremap, KVMLoongArchExtIOI, EXTIOI_IRQS), + VMSTATE_UINT32(features, KVMLoongArchExtIOI), + VMSTATE_UINT32(status, KVMLoongArchExtIOI), + VMSTATE_END_OF_LIST() + } +}; + +static Property extioi_properties[] =3D { + DEFINE_PROP_UINT32("num-cpu", KVMLoongArchExtIOI, num_cpu, 1), + DEFINE_PROP_BIT("has-virtualization-extension", KVMLoongArchExtIOI, + features, EXTIOI_HAS_VIRT_EXTENSION, 0), + DEFINE_PROP_END_OF_LIST(), +}; + +static void kvm_loongarch_extioi_class_init(ObjectClass *oc, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(oc); + KVMLoongArchExtIOIClass *extioi_class =3D KVM_LOONGARCH_EXTIOI_CLASS(o= c); + + extioi_class->parent_realize =3D dc->realize; + dc->realize =3D kvm_loongarch_extioi_realize; + extioi_class->is_created =3D false; + device_class_set_props(dc, extioi_properties); + dc->vmsd =3D &vmstate_kvm_extioi_core; +} + +static const TypeInfo kvm_loongarch_extioi_info =3D { + .name =3D TYPE_KVM_LOONGARCH_EXTIOI, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(KVMLoongArchExtIOI), + .class_size =3D sizeof(KVMLoongArchExtIOIClass), + .class_init =3D kvm_loongarch_extioi_class_init, +}; + +static void kvm_loongarch_extioi_register_types(void) +{ + type_register_static(&kvm_loongarch_extioi_info); +} + +type_init(kvm_loongarch_extioi_register_types) diff --git a/hw/intc/meson.build b/hw/intc/meson.build index f55eb1b80b..85174d1af1 100644 --- a/hw/intc/meson.build +++ b/hw/intc/meson.build @@ -76,3 +76,4 @@ specific_ss.add(when: 'CONFIG_LOONGARCH_IPI_KVM', if_true= : files('loongarch_ipi_ 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')) +specific_ss.add(when: 'CONFIG_LOONGARCH_EXTIOI_KVM', if_true: files('loong= arch_extioi_kvm.c')) diff --git a/hw/loongarch/Kconfig b/hw/loongarch/Kconfig index f8fcac3e7b..99a523171f 100644 --- a/hw/loongarch/Kconfig +++ b/hw/loongarch/Kconfig @@ -17,6 +17,7 @@ config LOONGARCH_VIRT select LOONGARCH_PCH_MSI select LOONGARCH_EXTIOI select LOONGARCH_IPI_KVM if KVM + select LOONGARCH_EXTIOI_KVM if KVM select LS7A_RTC select SMBIOS select ACPI_PCI diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c index 3b28e8e671..8ca7c09016 100644 --- a/hw/loongarch/virt.c +++ b/hw/loongarch/virt.c @@ -828,28 +828,37 @@ static void virt_irq_init(LoongArchVirtMachineState *= lvms) } =20 /* Create EXTIOI device */ - extioi =3D qdev_new(TYPE_LOONGARCH_EXTIOI); - qdev_prop_set_uint32(extioi, "num-cpu", ms->smp.cpus); - if (virt_is_veiointc_enabled(lvms)) { - qdev_prop_set_bit(extioi, "has-virtualization-extension", true); - } - sysbus_realize_and_unref(SYS_BUS_DEVICE(extioi), &error_fatal); - memory_region_add_subregion(&lvms->system_iocsr, APIC_BASE, - sysbus_mmio_get_region(SYS_BUS_DEVICE(extioi), 0)); - if (virt_is_veiointc_enabled(lvms)) { - memory_region_add_subregion(&lvms->system_iocsr, EXTIOI_VIRT_BASE, - sysbus_mmio_get_region(SYS_BUS_DEVICE(extioi), 1)); - } + if (kvm_enabled() && kvm_irqchip_in_kernel()) { + extioi =3D qdev_new(TYPE_KVM_LOONGARCH_EXTIOI); + qdev_prop_set_uint32(extioi, "num-cpu", ms->smp.cpus); + if (virt_is_veiointc_enabled(lvms)) { + qdev_prop_set_bit(extioi, "has-virtualization-extension", true= ); + } + sysbus_realize_and_unref(SYS_BUS_DEVICE(extioi), &error_fatal); + } else { + extioi =3D qdev_new(TYPE_LOONGARCH_EXTIOI); + qdev_prop_set_uint32(extioi, "num-cpu", ms->smp.cpus); + if (virt_is_veiointc_enabled(lvms)) { + qdev_prop_set_bit(extioi, "has-virtualization-extension", true= ); + } + sysbus_realize_and_unref(SYS_BUS_DEVICE(extioi), &error_fatal); + memory_region_add_subregion(&lvms->system_iocsr, APIC_BASE, + sysbus_mmio_get_region(SYS_BUS_DEVICE(extioi), 0)); + if (virt_is_veiointc_enabled(lvms)) { + memory_region_add_subregion(&lvms->system_iocsr, EXTIOI_VIRT_B= ASE, + sysbus_mmio_get_region(SYS_BUS_DEVICE(extioi), 1)); + } =20 - /* - * connect ext irq to the cpu irq - * cpu_pin[9:2] <=3D intc_pin[7:0] - */ - for (cpu =3D 0; cpu < ms->smp.cpus; cpu++) { - cpudev =3D DEVICE(qemu_get_cpu(cpu)); - for (pin =3D 0; pin < LS3A_INTC_IP; pin++) { - qdev_connect_gpio_out(extioi, (cpu * 8 + pin), - qdev_get_gpio_in(cpudev, pin + 2)); + /* + * connect ext irq to the cpu irq + * cpu_pin[9:2] <=3D intc_pin[7:0] + */ + for (cpu =3D 0; cpu < ms->smp.cpus; cpu++) { + cpudev =3D DEVICE(qemu_get_cpu(cpu)); + for (pin =3D 0; pin < LS3A_INTC_IP; pin++) { + qdev_connect_gpio_out(extioi, (cpu * 8 + pin), + qdev_get_gpio_in(cpudev, pin + 2)); + } } } =20 --=20 2.39.1