Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp15845872rwd; Mon, 26 Jun 2023 02:03:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ481qwimeGdQjAbE7XBQ0OkQkMUfyXaJjGThyoaoacBrM6Be4z3Glv6UJNEQlvx0DVJH1e6 X-Received: by 2002:a05:6402:498:b0:519:b784:b157 with SMTP id k24-20020a056402049800b00519b784b157mr17774156edv.12.1687770213725; Mon, 26 Jun 2023 02:03:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687770213; cv=none; d=google.com; s=arc-20160816; b=JdBAaEO8pGUO+AlfmxAH6xdsLMkEmdyIyS2Ay2+46APP+RY9lBmKKJjj/YJEKvMFnh 6C5ZJof9O6VJBEeTvq/UP2Atmscm3JVtLcarm6CHbWN17hSlIuEm4RnYK4iCnEtIAMcS UO5f4GQZi8gO0vjdc+HbDObAG3GdraOxZBrfDTqOwUpwa59g+1IsbdxnIAPrVuDXZVq5 Nx7CVRybk05NclwoCoZKM/F5gcxXKyHU+47MJ1daUV2NEJcFLhFA5r3jDEALD2mvgm4b 4DWtG+ALna6OPSJGYcC+ccRyqhJ0oCwITfw5AGADVWBvTgmKy+gGb1YOrokYqf4G/f3L a/Ow== 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=2znVeupWncUBd1dOjue+03kabl5V1PLMRlmvN1G04mg=; fh=FJJE1KfQOXNgUCxk/Gxke8tBG9gBLo3aFI+sNzAMBeU=; b=laZDLhb4FYbEVerWJlMoFvLuGojAkuuHriwW7Nqdj++iTkhkPhUfzIpMW+VGyCXcqD 3yy2+YoXQo7n74KeNp8VwgjMFOceSkO0bWmC7yIEOHAg1KsuaeONbG4Zh5g1fzmqpmYr 3Go58lMFapu6iKVA3GiRXWLRJMNOuhwYtyBImL5PBFtMnh6L38YitIv29sVMYgalU9Gw eJfWdXVKrQ2MddA8Xcm/5t7jgWzICUHLW5R3psaX31gSPX7AjQvYb06jZJtofaxBM6Ij uTqNi2FKEEhe/10onTSG7FhxgdumJ9Tjji2I/p4k3mW0ddVU2drz5LF+DptUBXQk1pSe vTJQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z9-20020aa7cf89000000b0051a3702d11fsi2430675edx.621.2023.06.26.02.03.09; Mon, 26 Jun 2023 02:03:33 -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 S230382AbjFZIs7 (ORCPT + 99 others); Mon, 26 Jun 2023 04:48:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229892AbjFZIsO (ORCPT ); Mon, 26 Jun 2023 04:48:14 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 202C31A4; Mon, 26 Jun 2023 01:48:03 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8DxPMe_UJlkmTQCAA--.3269S3; Mon, 26 Jun 2023 16:47:59 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bxxsy5UJlk4hQJAA--.19269S13; Mon, 26 Jun 2023 16:47:58 +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, tangyouling@loongson.cn Subject: [PATCH v15 11/30] LoongArch: KVM: Implement fpu related operations for vcpu Date: Mon, 26 Jun 2023 16:47:33 +0800 Message-Id: <20230626084752.1138621-12-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230626084752.1138621-1-zhaotianrui@loongson.cn> References: <20230626084752.1138621-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Bxxsy5UJlk4hQJAA--.19269S13 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,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 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 da97b77da8eb..71f5e0fb96e5 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -237,6 +237,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; -- 2.39.1