Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4284863rdb; Thu, 14 Sep 2023 18:54:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHybOdZpBsToudN0hFFCx4HJj1lqG23G0s+O1qfAe5KU9LFod8vlwTJ52J1BJASQ3v9JfqQ X-Received: by 2002:a05:6a21:a59b:b0:13e:7d3:61d1 with SMTP id gd27-20020a056a21a59b00b0013e07d361d1mr759976pzc.12.1694742870761; Thu, 14 Sep 2023 18:54:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694742870; cv=none; d=google.com; s=arc-20160816; b=NYP2yXrFXUfvI1LggNcK8gDHD2OsE/3+FBGIgY2xMmALPPuJCShLJHCi6uu/BmGChY sdmiAx0DStoj1QZPlR7gC+58CBoiPpaVPxne6qqgzFF7o/lFHRF2eMhuSELuaEbDcbdR UFgnsN4gts53gcXmwlPs6aCNWeIoCYzBGhCRt3V8FTk872pK2It49RBV3fH3fOJ7YSpT sFjuykjdwmcFRy4UutbSd+zJ5zz8LH7/yCeNb1RGdyHBlm/lhlXA/DnO0bZ5+UIxcUQ6 nr0i7ACVnbKmho2V5H+kwmcDo6wZRDutK86VW7vCLLqQ43/CcluifNFkvPD+RCzyz7Ot rqFg== 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=/r6K7pcbMn0uOQ6x9llxoauG7VqHYbEuLkmpQgGcZlM=; fh=ejoaVAUUbBXsA/6jM/beuI9xCYtV2osdR9SpI7hjY84=; b=hysOjUNyDkbvFuO2CVpPcL9LGGNqdci8ylCiAGvX0SyQ3ECaXXiulU7x68nFBAmCdX +8zJcsg5PUXoRjE9YD/mQCTIlL2gCSm1dAkHG4KCwJVmZd2aNivA9/pSr2RFBEoRw0GU XJICqZZHLOyqFb49gffUU+0XA0AGI02rZau53OJQF/PeizNL2TEF4jwdZg0c5RcuprTE MuOusjJRQIrFAVgvPfFO9qIldJFckiyECgpMXVEbQXrxvU4c2sTaeVlyqvXCttOuLANM 1wvueZUw+PppeXYv5uu73RweRODAF+4btOxMaysTs7OHsijHwfgRwI2FS57OCjdE63Q3 /Arw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id j22-20020a056a00235600b0069026a3de0asi2521170pfj.138.2023.09.14.18.54.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 18:54:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 66EC7853ADEC; Thu, 14 Sep 2023 18:50:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231496AbjIOBuX (ORCPT + 99 others); Thu, 14 Sep 2023 21:50:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231358AbjIOBuH (ORCPT ); Thu, 14 Sep 2023 21:50:07 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4D5E02708; Thu, 14 Sep 2023 18:50:02 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8CxbetFuANl2P4nAA--.9231S3; Fri, 15 Sep 2023 09:49:57 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Axndw9uANl+ioGAA--.11927S13; Fri, 15 Sep 2023 09:49:57 +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 v21 11/29] LoongArch: KVM: Implement fpu related operations for vcpu Date: Fri, 15 Sep 2023 09:49:31 +0800 Message-Id: <20230915014949.1222777-12-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230915014949.1222777-1-zhaotianrui@loongson.cn> References: <20230915014949.1222777-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Axndw9uANl+ioGAA--.11927S13 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 14 Sep 2023 18:50:25 -0700 (PDT) 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 6b1b2a058b..b8af4a200c 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -276,6 +276,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