From nobody Sat Nov 30 10:29:27 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 41F73193071; Tue, 10 Sep 2024 12:01:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725969716; cv=none; b=No00oXpEG+UKP3XZc6pcbuBF78XE3BkF4vkzrRiqQlCI7g/bDpB5sbZPB3jreNe2Y2z2iA1QT1TauIW3WBayiUuc3jEvfb94BLCBI8H6HBHvoQwOpQSV8oet0Sq4WjDrvyr0HjwUulO6XtdgxI71WIfJvo3pq0OWkAONg00/ims= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725969716; c=relaxed/simple; bh=AfBRkH/I6+8rMVFKzivau/g8opk+pqEpfSrCnIx/B84=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rzIEbW2G04HgdJMX1Qh8rf2HrsCU3J9dImYWlTY7FS4Hr/5UGWV5e8vzDAXUBo9hRLEEJPRYAst7e9qRZF9WmcrJGYipKZbQDTvyY3HYVCtkM58kxHjBDTUBgEmuMlgnylzduW4oYyxilpV9uXxP9wIlyp/ouwyX+ZPAwYK3X+E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8CxhegsNeBmJ68DAA--.7612S3; Tue, 10 Sep 2024 20:01:48 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by front2 (Coremail) with SMTP id qciowMCxrsceNeBmyGIDAA--.15904S5; Tue, 10 Sep 2024 20:01:46 +0800 (CST) From: Xianglai Li To: linux-kernel@vger.kernel.org Cc: Min Zhou , Tianrui Zhao , Bibo Mao , Huacai Chen , kvm@vger.kernel.org, loongarch@lists.linux.dev, Paolo Bonzini , WANG Xuerui , Xianglai li Subject: [PATCH V3 03/11] LoongArch: KVM: Add IPI read and write function Date: Tue, 10 Sep 2024 19:43:52 +0800 Message-Id: <20240910114400.4062433-4-lixianglai@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20240910114400.4062433-1-lixianglai@loongson.cn> References: <20240910114400.4062433-1-lixianglai@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qciowMCxrsceNeBmyGIDAA--.15904S5 X-CM-SenderInfo: 5ol0xt5qjotxo6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" Implementation of IPI interrupt controller address space read and write function simulation. Signed-off-by: Min Zhou Signed-off-by: Tianrui Zhao Signed-off-by: Xianglai Li --- Cc: Bibo Mao =20 Cc: Huacai Chen =20 Cc: kvm@vger.kernel.org=20 Cc: loongarch@lists.linux.dev=20 Cc: Paolo Bonzini =20 Cc: Tianrui Zhao =20 Cc: WANG Xuerui =20 Cc: Xianglai li =20 arch/loongarch/include/asm/kvm_host.h | 2 + arch/loongarch/include/asm/kvm_ipi.h | 16 ++ arch/loongarch/kvm/intc/ipi.c | 292 +++++++++++++++++++++++++- 3 files changed, 307 insertions(+), 3 deletions(-) diff --git a/arch/loongarch/include/asm/kvm_host.h b/arch/loongarch/include= /asm/kvm_host.h index 2b65007503dc..7c89e26c23c3 100644 --- a/arch/loongarch/include/asm/kvm_host.h +++ b/arch/loongarch/include/asm/kvm_host.h @@ -44,6 +44,8 @@ struct kvm_vm_stat { struct kvm_vm_stat_generic generic; u64 pages; u64 hugepages; + u64 ipi_read_exits; + u64 ipi_write_exits; }; =20 struct kvm_vcpu_stat { diff --git a/arch/loongarch/include/asm/kvm_ipi.h b/arch/loongarch/include/= asm/kvm_ipi.h index 3dacdf1781b8..ce10554fb502 100644 --- a/arch/loongarch/include/asm/kvm_ipi.h +++ b/arch/loongarch/include/asm/kvm_ipi.h @@ -29,8 +29,24 @@ struct ipi_state { #define SMP_MAILBOX 0x1000 #define KVM_IOCSR_IPI_ADDR_SIZE 0x48 =20 +#define CORE_STATUS_OFF 0x000 +#define CORE_EN_OFF 0x004 +#define CORE_SET_OFF 0x008 +#define CORE_CLEAR_OFF 0x00c +#define CORE_BUF_20 0x020 +#define CORE_BUF_28 0x028 +#define CORE_BUF_30 0x030 +#define CORE_BUF_38 0x038 +#define IOCSR_IPI_SEND 0x040 + +#define IOCSR_MAIL_SEND 0x048 +#define IOCSR_ANY_SEND 0x158 + #define MAIL_SEND_ADDR (SMP_MAILBOX + IOCSR_MAIL_SEND) #define KVM_IOCSR_MAIL_ADDR_SIZE 0x118 =20 +#define MAIL_SEND_OFFSET 0 +#define ANY_SEND_OFFSET (IOCSR_ANY_SEND - IOCSR_MAIL_SEND) + int kvm_loongarch_register_ipi_device(void); #endif diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c index 5a19712185d5..2620c1500078 100644 --- a/arch/loongarch/kvm/intc/ipi.c +++ b/arch/loongarch/kvm/intc/ipi.c @@ -7,25 +7,311 @@ #include #include =20 +static void ipi_send(struct kvm *kvm, uint64_t data) +{ + struct kvm_vcpu *vcpu; + struct kvm_interrupt irq; + int cpu, action, status; + + cpu =3D ((data & 0xffffffff) >> 16) & 0x3ff; + vcpu =3D kvm_get_vcpu_by_cpuid(kvm, cpu); + if (unlikely(vcpu =3D=3D NULL)) { + kvm_err("%s: invalid target cpu: %d\n", __func__, cpu); + return; + } + + action =3D BIT(data & 0x1f); + spin_lock(&vcpu->arch.ipi_state.lock); + status =3D vcpu->arch.ipi_state.status; + vcpu->arch.ipi_state.status |=3D action; + spin_unlock(&vcpu->arch.ipi_state.lock); + if (status =3D=3D 0) { + irq.irq =3D LARCH_INT_IPI; + kvm_vcpu_ioctl_interrupt(vcpu, &irq); + } +} + +static void ipi_clear(struct kvm_vcpu *vcpu, uint64_t data) +{ + struct kvm_interrupt irq; + uint32_t status; + + spin_lock(&vcpu->arch.ipi_state.lock); + vcpu->arch.ipi_state.status &=3D ~data; + status =3D vcpu->arch.ipi_state.status; + spin_unlock(&vcpu->arch.ipi_state.lock); + if (!status) { + irq.irq =3D -LARCH_INT_IPI; + kvm_vcpu_ioctl_interrupt(vcpu, &irq); + } +} + +static uint64_t read_mailbox(struct kvm_vcpu *vcpu, int offset, int len) +{ + uint64_t ret =3D 0; + uint64_t data =3D 0; + + spin_lock(&vcpu->arch.ipi_state.lock); + data =3D *(ulong *)((void *)vcpu->arch.ipi_state.buf + (offset - 0x20)); + spin_unlock(&vcpu->arch.ipi_state.lock); + + if (len =3D=3D 1) + ret =3D data & 0xff; + else if (len =3D=3D 2) + ret =3D data & 0xffff; + else if (len =3D=3D 4) + ret =3D data & 0xffffffff; + else if (len =3D=3D 8) + ret =3D data; + else + kvm_err("%s: unknown data len: %d\n", __func__, len); + return ret; +} + +static void write_mailbox(struct kvm_vcpu *vcpu, int offset, + uint64_t data, int len) +{ + void *pbuf; + bool bad_width =3D false; + + spin_lock(&vcpu->arch.ipi_state.lock); + pbuf =3D (void *)vcpu->arch.ipi_state.buf + (offset - 0x20); + if (len =3D=3D 1) + *(unsigned char *)pbuf =3D (unsigned char)data; + else if (len =3D=3D 2) + *(unsigned short *)pbuf =3D (unsigned short)data; + else if (len =3D=3D 4) + *(unsigned int *)pbuf =3D (unsigned int)data; + else if (len =3D=3D 8) + *(unsigned long *)pbuf =3D (unsigned long)data; + else + bad_width =3D true; + spin_unlock(&vcpu->arch.ipi_state.lock); + if (bad_width) + kvm_err("%s: unknown data len: %d\n", __func__, len); +} + +static int loongarch_ipi_writel(struct kvm_vcpu *vcpu, gpa_t addr, + int len, const void *val) +{ + uint64_t data; + uint32_t offset; + int ret =3D 0; + + data =3D *(uint64_t *)val; + + offset =3D (uint32_t)(addr & 0xff); + WARN_ON_ONCE(offset & (len - 1)); + + switch (offset) { + case CORE_STATUS_OFF: + kvm_err("CORE_SET_OFF Can't be write\n"); + ret =3D -EINVAL; + break; + case CORE_EN_OFF: + spin_lock(&vcpu->arch.ipi_state.lock); + vcpu->arch.ipi_state.en =3D data; + spin_unlock(&vcpu->arch.ipi_state.lock); + break; + case IOCSR_IPI_SEND: + ipi_send(vcpu->kvm, data); + break; + case CORE_SET_OFF: + ret =3D -EINVAL; + break; + case CORE_CLEAR_OFF: + /* Just clear the status of the current vcpu */ + ipi_clear(vcpu, data); + break; + case CORE_BUF_20 ... CORE_BUF_38 + 7: + if (offset + len > CORE_BUF_38 + 8) { + kvm_err("%s: invalid offset or len: offset =3D %d, len =3D %d\n", + __func__, offset, len); + ret =3D -EINVAL; + break; + } + write_mailbox(vcpu, offset, data, len); + break; + default: + kvm_err("%s: unknown addr: %llx\n", __func__, addr); + ret =3D -EINVAL; + break; + } + return ret; +} + +static int loongarch_ipi_readl(struct kvm_vcpu *vcpu, gpa_t addr, + int len, void *val) +{ + uint32_t offset; + uint64_t res =3D 0; + int ret =3D 0; + + offset =3D (uint32_t)(addr & 0xff); + WARN_ON_ONCE(offset & (len - 1)); + + switch (offset) { + case CORE_STATUS_OFF: + spin_lock(&vcpu->arch.ipi_state.lock); + res =3D vcpu->arch.ipi_state.status; + spin_unlock(&vcpu->arch.ipi_state.lock); + break; + case CORE_EN_OFF: + spin_lock(&vcpu->arch.ipi_state.lock); + res =3D vcpu->arch.ipi_state.en; + spin_unlock(&vcpu->arch.ipi_state.lock); + break; + case CORE_SET_OFF: + res =3D 0; + break; + case CORE_CLEAR_OFF: + res =3D 0; + break; + case CORE_BUF_20 ... CORE_BUF_38 + 7: + if (offset + len > CORE_BUF_38 + 8) { + kvm_err("%s: invalid offset or len: offset =3D %d, len =3D %d\n", + __func__, offset, len); + ret =3D -EINVAL; + break; + } + res =3D read_mailbox(vcpu, offset, len); + break; + default: + kvm_err("%s: unknown addr: %llx\n", __func__, addr); + ret =3D -EINVAL; + break; + } + *(uint64_t *)val =3D res; + return ret; +} + static int kvm_ipi_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, gpa_t addr, int len, const void *val) { - return 0; + struct loongarch_ipi *ipi; + int ret; + + ipi =3D vcpu->kvm->arch.ipi; + if (!ipi) { + kvm_err("%s: ipi irqchip not valid!\n", __func__); + return -EINVAL; + } + ipi->kvm->stat.ipi_write_exits++; + ret =3D loongarch_ipi_writel(vcpu, addr, len, val); + return ret; } =20 static int kvm_ipi_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, gpa_t addr, int len, void *val) { - return 0; + struct loongarch_ipi *ipi; + int ret; + + ipi =3D vcpu->kvm->arch.ipi; + if (!ipi) { + kvm_err("%s: ipi irqchip not valid!\n", __func__); + return -EINVAL; + } + ipi->kvm->stat.ipi_read_exits++; + ret =3D loongarch_ipi_readl(vcpu, addr, len, val); + return ret; +} + +static int send_ipi_data(struct kvm_vcpu *vcpu, gpa_t addr, uint64_t data) +{ + int i, ret; + uint32_t val =3D 0, mask =3D 0; + /* + * Bit 27-30 is mask for byte writing. + * If the mask is 0, we need not to do anything. + */ + if ((data >> 27) & 0xf) { + /* Read the old val */ + ret =3D kvm_io_bus_read(vcpu, KVM_IOCSR_BUS, addr, sizeof(val), &val); + if (unlikely(ret)) { + kvm_err("%s: : read date from addr %llx failed\n", __func__, addr); + return ret; + } + /* Construct the mask by scanning the bit 27-30 */ + for (i =3D 0; i < 4; i++) { + if (data & (BIT(27 + i))) + mask |=3D (0xff << (i * 8)); + } + /* Save the old part of val */ + val &=3D mask; + } + val |=3D ((uint32_t)(data >> 32) & ~mask); + ret =3D kvm_io_bus_write(vcpu, KVM_IOCSR_BUS, addr, sizeof(val), &val); + if (unlikely(ret)) + kvm_err("%s: : write date to addr %llx failed\n", __func__, addr); + return ret; +} + +static int mail_send(struct kvm *kvm, uint64_t data) +{ + struct kvm_vcpu *vcpu; + int cpu, mailbox; + int offset, ret; + + cpu =3D ((data & 0xffffffff) >> 16) & 0x3ff; + vcpu =3D kvm_get_vcpu_by_cpuid(kvm, cpu); + if (unlikely(vcpu =3D=3D NULL)) { + kvm_err("%s: invalid target cpu: %d\n", __func__, cpu); + return -EINVAL; + } + mailbox =3D ((data & 0xffffffff) >> 2) & 0x7; + offset =3D SMP_MAILBOX + CORE_BUF_20 + mailbox * 4; + ret =3D send_ipi_data(vcpu, offset, data); + return ret; +} + +static int any_send(struct kvm *kvm, uint64_t data) +{ + struct kvm_vcpu *vcpu; + int cpu, offset, ret; + + cpu =3D ((data & 0xffffffff) >> 16) & 0x3ff; + vcpu =3D kvm_get_vcpu_by_cpuid(kvm, cpu); + if (unlikely(vcpu =3D=3D NULL)) { + kvm_err("%s: invalid target cpu: %d\n", __func__, cpu); + return -EINVAL; + } + offset =3D data & 0xffff; + ret =3D send_ipi_data(vcpu, offset, data); + return ret; } =20 static int kvm_loongarch_mail_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, gpa_t addr, int len, const void *val) { - return 0; + struct loongarch_ipi *ipi; + int ret; + + ipi =3D vcpu->kvm->arch.ipi; + if (!ipi) { + kvm_err("%s: ipi irqchip not valid!\n", __func__); + return -EINVAL; + } + + addr &=3D 0xfff; + addr -=3D IOCSR_MAIL_SEND; + + switch (addr) { + case MAIL_SEND_OFFSET: + ret =3D mail_send(vcpu->kvm, *(uint64_t *)val); + break; + case ANY_SEND_OFFSET: + ret =3D any_send(vcpu->kvm, *(uint64_t *)val); + break; + default: + kvm_err("%s: invalid addr %llx!\n", __func__, addr); + ret =3D -EINVAL; + break; + } + return ret; } =20 static const struct kvm_io_device_ops kvm_ipi_ops =3D { --=20 2.39.1