From patchwork Thu Aug 31 08:30:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 137259 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp127171vqu; Thu, 31 Aug 2023 02:42:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGHhAQZwwMS3ift3UH+zdrXqgKTXm/809NZbi0sV4Hkp9DYLaBx1KVqTG6BZ139aOamMzAs X-Received: by 2002:a17:906:3299:b0:9a5:8155:6de with SMTP id 25-20020a170906329900b009a5815506demr3578170ejw.45.1693474927580; Thu, 31 Aug 2023 02:42:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693474927; cv=none; d=google.com; s=arc-20160816; b=GbVscLwkzQMLb0ESkJwNzIIDWbAeiItUh98pqLCWq9lNtunC+c4msS1lBDW3tvDou2 8rdPElbQ4gmrVi/qi40hPNppo0X5xS2nSBi/QyXedm585DxF04RgHGhiSbc8kgOKlypg AflQApB+hopNwzDbKgwxuzBi2R202RYvUlok6p3Uq8t/zE/JJVRqlLZH023AaOJIOZa8 2paKjxBrsfFdQxbjsVjoqUmQSGAN2jTINui0t28LqfdOYqEuIn67os8gONKLHz7LXfwG gF7EnQVhXEXd21Y/xT54x+aktJikBFcss02A8+MkcWrhU2SkVjgWO5Nbx0jnB4TLxI3X dZdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2sEAVxksvfcmbQphWB6a4uSl8iiSMwUT65LbOXx2mE0=; fh=ejoaVAUUbBXsA/6jM/beuI9xCYtV2osdR9SpI7hjY84=; b=J+WLgseTz7nQngdYeqAprFKLbtz8eR8q3ep1w/iynHO9tuU6lVsds5K5E8c7gq7VXg hUhIMZPYE1G0MhQAV9bAFrPLtmce5QIHlFkstPXejz9/gHsETlMsIcgUYtCRXcx3rCWE lF4KA2jW4vAehBj96asLiTYyqqvbM8ZZYKafv1DcPkwOIdxwq+8yAbqV2ct1fSZeRFph b0+/f6M4qqstsH7uDdbSsqahvVoU9RAGVyt1WhsLVFAhNPQC/dmgwfEF/3sWIkN/Zduq FQolzdi+nNPbebAsRahoLEN9NoaY4FbTm0zyyFyeIjsZJVJDT5aD5BkCf04s+fHTlg8S BVig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rv20-20020a17090710d400b0098d7e44a637si789730ejb.794.2023.08.31.02.41.30; Thu, 31 Aug 2023 02:42:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345204AbjHaIb1 (ORCPT + 99 others); Thu, 31 Aug 2023 04:31:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242341AbjHaIax (ORCPT ); Thu, 31 Aug 2023 04:30:53 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5B84BE48; Thu, 31 Aug 2023 01:30:38 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8DxFeinT_BkS14dAA--.1000S3; Thu, 31 Aug 2023 16:30:31 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Ax3c6gT_BkOPVnAA--.55892S13; Thu, 31 Aug 2023 16:30:30 +0800 (CST) From: Tianrui Zhao To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao , zhaotianrui@loongson.cn Subject: [PATCH v20 11/30] LoongArch: KVM: Implement fpu related operations for vcpu Date: Thu, 31 Aug 2023 16:30:01 +0800 Message-Id: <20230831083020.2187109-12-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230831083020.2187109-1-zhaotianrui@loongson.cn> References: <20230831083020.2187109-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Ax3c6gT_BkOPVnAA--.55892S13 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775737165214238058 X-GMAIL-MSGID: 1775737165214238058 Implement LoongArch fpu related interface for vcpu, such as get fpu, set fpu, own fpu and lose fpu, etc. Reviewed-by: Bibo Mao Signed-off-by: Tianrui Zhao --- arch/loongarch/kvm/vcpu.c | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index be0c17a433..2094afcfcd 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -238,6 +238,66 @@ long kvm_arch_vcpu_ioctl(struct file *filp, return r; } +int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) +{ + int i = 0; + + /* no need vcpu_load and vcpu_put */ + fpu->fcsr = vcpu->arch.fpu.fcsr; + fpu->fcc = vcpu->arch.fpu.fcc; + for (i = 0; i < NUM_FPU_REGS; i++) + memcpy(&fpu->fpr[i], &vcpu->arch.fpu.fpr[i], FPU_REG_WIDTH / 64); + + return 0; +} + +int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) +{ + int i = 0; + + /* no need vcpu_load and vcpu_put */ + vcpu->arch.fpu.fcsr = fpu->fcsr; + vcpu->arch.fpu.fcc = fpu->fcc; + for (i = 0; i < NUM_FPU_REGS; i++) + memcpy(&vcpu->arch.fpu.fpr[i], &fpu->fpr[i], FPU_REG_WIDTH / 64); + + return 0; +} + +/* Enable FPU for guest and restore context */ +void kvm_own_fpu(struct kvm_vcpu *vcpu) +{ + preempt_disable(); + + /* + * Enable FPU for guest + */ + set_csr_euen(CSR_EUEN_FPEN); + + kvm_restore_fpu(&vcpu->arch.fpu); + vcpu->arch.aux_inuse |= KVM_LARCH_FPU; + trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU); + + preempt_enable(); +} + +/* Save and disable FPU */ +void kvm_lose_fpu(struct kvm_vcpu *vcpu) +{ + preempt_disable(); + + if (vcpu->arch.aux_inuse & KVM_LARCH_FPU) { + kvm_save_fpu(&vcpu->arch.fpu); + vcpu->arch.aux_inuse &= ~KVM_LARCH_FPU; + trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU); + + /* Disable FPU */ + clear_csr_euen(CSR_EUEN_FPEN); + } + + preempt_enable(); +} + int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) { return 0;