From patchwork Mon Apr 17 09:59:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 84135 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2012394vqo; Mon, 17 Apr 2023 03:22:40 -0700 (PDT) X-Google-Smtp-Source: AKy350Z7k59/ewvUVeWZaubIZCuvDqwmSYNbHXKCsewqdBrIWTuC2DitHFDZnq9rOJX7dvXd2jGw X-Received: by 2002:a17:90b:23d8:b0:233:fdfd:7122 with SMTP id md24-20020a17090b23d800b00233fdfd7122mr14812237pjb.37.1681726959787; Mon, 17 Apr 2023 03:22:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681726959; cv=none; d=google.com; s=arc-20160816; b=O+0wi7f9w41e72rNDbosU3yQxHtCv9c6dd1h6YiZrhOrX9Mt1tiz3LNMRQMGhbLj30 uDRLb8DXKw8gPPOa1jZ6SMUiZV2FYVD6HzK6b9Ag6CO0qg31S4sAYZQCoN7IQhSXgwrs dG3UYI/kZHZxTvAmCnC24dr4UL19L/HEtpSgQDLhfwn9MdAPTrpklFmAdpSW2n7lqLhY v6rzB+WcdzYpehlkX3eHhQ+DaRyN+nlPqnmstrU4ckuDI8/mh0hH8/qdpkllvY623hWC IkmTf/QYrI9lDWrbnuUhszfJ+RtF8+Gav1mNGCCaI6SSUCUgNWxkx0qWFbOjmgJ72JSu klNw== 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=X4Tkw6irboUsDUC+HmEzUj7MJeZPpYBAJYvrThBNKyU=; b=BlFfAVxY3lrAPAl4wxFnTchzlYXp4CKBj0kGTudHChBfyEfsyzykF/rwjFRgi8gU7h 2yzSgAmGnyOD2scsmRNh1LaCxde40G2nUPA855zU4y7PBnQQIeFJQhQJV27xVDR2YHc8 VCmyoO0GI3nOhjGseLj+/5HE6oQltkzlmH8pT4F/R09YoUalPf2Zxi1fJP0igSnw9OR3 hMMcWzYQaBTUj12Hk3+ZBjvr6U7aCmFrN7gD1iaHjxq7dBLq0J4AfCrZUKNiUW4tcIvh j3TvAmzUrtzqJWMNxrobaiW+8NU8nmo28MgjOEFpaI1wIgRyGek4gQXHkcuZ8fbbLkBo nfPg== 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 i11-20020a17090a974b00b002477fad79d5si3649476pjw.63.2023.04.17.03.22.25; Mon, 17 Apr 2023 03:22:39 -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 S230128AbjDQKBj (ORCPT + 99 others); Mon, 17 Apr 2023 06:01:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229681AbjDQKBW (ORCPT ); Mon, 17 Apr 2023 06:01:22 -0400 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 16F441BCB; Mon, 17 Apr 2023 03:00:40 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8AxlF2aGD1kO+YdAA--.46195S3; Mon, 17 Apr 2023 17:59:54 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bx57yWGD1kua0qAA--.48254S13; Mon, 17 Apr 2023 17:59:53 +0800 (CST) From: Tianrui Zhao To: Paolo Bonzini Cc: Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao , zhaotianrui@loongson.cn Subject: [PATCH v7 11/30] LoongArch: KVM: Implement fpu related operations for vcpu Date: Mon, 17 Apr 2023 17:59:31 +0800 Message-Id: <20230417095950.875228-12-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230417095950.875228-1-zhaotianrui@loongson.cn> References: <20230417095950.875228-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Bx57yWGD1kua0qAA--.48254S13 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoW7KF1UXF4kKw4xXw1rWryxAFb_yoW8ur1kpF W7Ar90qayrG3WxK34aqr1jgr1avrWkKr1xXa47WryfAr1UtryrZF4vkrW2vF98Jw1xZFyI vF1rGF1UCFyDAwUanT9S1TB71UUUU8DqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU bcAFc2x0x2IEx4CE42xK8VAvwI8IcIk0rVWrJVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4 AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF 7I0E14v26F4UJVW0owA2z4x0Y4vEx4A2jsIE14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7 CjxVAFwI0_GcCE3s1ln4kS14v26r4a6rW5M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF 6xkI12xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x8ErcxFaVAv8V WrMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCY1x0262kKe7AKxVW8ZVWr XwCF04k20xvY0x0EwIxGrwCF04k20xvE74AGY7Cv6cx26rWl4I8I3I0E4IkC6x0Yz7v_Jr 0_Gr1l4IxYO2xFxVAFwI0_GFv_Wrylx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK 67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI 8IcVAFwI0_tr0E3s1lIxAIcVC0I7IYx2IY6xkF7I0E14v26F4UJVW0owCI42IY6xAIw20E Y4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr1j6F4UJwCI42IY6I8E87Iv6xkF7I 0E14v26F4UJVW0obIYCTnIWIevJa73UjIFyTuYvjxU42YLDUUUU X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763418528462790649?= X-GMAIL-MSGID: =?utf-8?q?1763418528462790649?= Implement loongarch fpu related interface for vcpu, such as get fpu, set fpu, own fpu and lose fpu, etc. 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 e1e6167699de..d1e8cd402900 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -245,6 +245,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;