From patchwork Fri Jun 9 09:08:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 105460 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp814235vqr; Fri, 9 Jun 2023 02:17:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7qHPAXLUzFCl2Rs87ZIWIsf44GRnG4+UmLGJy21yQBEqnuYp9LCe5ddM3/MkUjGkovUUb0 X-Received: by 2002:a05:6808:d51:b0:396:4977:e148 with SMTP id w17-20020a0568080d5100b003964977e148mr1184601oik.9.1686302223826; Fri, 09 Jun 2023 02:17:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686302223; cv=none; d=google.com; s=arc-20160816; b=TA2AmOX3CzuKspDpiPeBgbq0cgq8fEPjR6MUNiAg6PavojnJBP8IOywyMTFfpNFdph +o+R2Iuy+b4oTlE2axQBOZUncymPUHV6py0I1N1Exka4ob0uIMQAXXVXWXcenXNhdRFG fiIfIEP8fA/rtVCv/4n6gL8NZ3HMJWULbhavyMeBMYtQsL2bunGH/qw9qZnmgCwxzMUc r1MW/p1wom14cIc+/raGsu+br+ht1/r6a4jm2s9hMRqlqbBKt7At470pkaQdpsqn1GPE SID4lno1tHfTjLDPrI9097XR+ccM6DkrguqMXAu+EdS0wPyDmA5I9eypJoJA2QWq40No Q/9g== 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=4ntL5Lh/ieW9vdwgCl/BuRnbzmWUgI095hEJD1zKud4=; b=Fle6KOl68Y7iaj9fhCtQvLpqTQrTqHMMbEP98iIu8a7rS25MroENWgSvDGT+gS3QlW EQjDmbAqNKrH5CiURY2TA2oaTR1iGVgDQMxht29Mpowdv3/xPsGtnMrbD0inNJG/SLiw gYjn36M6VhMqgmlqPAtyEwJBgz0SHeYFNZu+HkshyIe9z709Ow2SWlv0K0PTaB2NbDB1 J5/5J20XYje2Ziy+iRe9GMSgDUpCCXotaQHRD7GnfStC5kKnpqzNGXoxLg+7o1Q75Lbj FUpbtEaZ5IVgZhnyQjE8vtTTnkqUQ0dVkMtlFKoFoVrLfYEvBVSlQJYl2dtY1uwEMqVs sFaA== 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 2-20020a630202000000b00546838027aasi2266077pgc.829.2023.06.09.02.16.51; Fri, 09 Jun 2023 02:17:03 -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 S241401AbjFIJO1 (ORCPT + 99 others); Fri, 9 Jun 2023 05:14:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241364AbjFIJOF (ORCPT ); Fri, 9 Jun 2023 05:14:05 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E24F95587; Fri, 9 Jun 2023 02:09:45 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Ax3eoU7IJk9fwAAA--.2994S3; Fri, 09 Jun 2023 17:08:36 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxFOQQ7IJkzHQKAA--.31790S5; Fri, 09 Jun 2023 17:08:35 +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 v13 03/30] LoongArch: KVM: Implement kvm hardware enable, disable interface Date: Fri, 9 Jun 2023 17:08:05 +0800 Message-Id: <20230609090832.2131037-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230609090832.2131037-1-zhaotianrui@loongson.cn> References: <20230609090832.2131037-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8DxFOQQ7IJkzHQKAA--.31790S5 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768216040347984359?= X-GMAIL-MSGID: =?utf-8?q?1768216040347984359?= Implement kvm hardware enable, disable interface, setting the guest config register to enable virtualization features when called the interface. Signed-off-by: Tianrui Zhao Reviewed-by: Bibo Mao --- arch/loongarch/kvm/main.c | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c index f98c1619725f..5ebae1ea7565 100644 --- a/arch/loongarch/kvm/main.c +++ b/arch/loongarch/kvm/main.c @@ -195,6 +195,70 @@ static void _kvm_init_gcsr_flag(void) set_gcsr_sw_flag(LOONGARCH_CSR_PERFCNTR3); } +void kvm_init_vmcs(struct kvm *kvm) +{ + kvm->arch.vmcs = vmcs; +} + +long kvm_arch_dev_ioctl(struct file *filp, + unsigned int ioctl, unsigned long arg) +{ + return -ENOIOCTLCMD; +} + +#ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING +int kvm_arch_hardware_enable(void) +{ + unsigned long env, gcfg = 0; + + env = read_csr_gcfg(); + /* First init gtlbc, gcfg, gstat, gintc. All guest use the same config */ + clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI); + write_csr_gcfg(0); + write_csr_gstat(0); + write_csr_gintc(0); + + /* + * Enable virtualization features granting guest direct control of + * certain features: + * GCI=2: Trap on init or unimplement cache instruction. + * TORU=0: Trap on Root Unimplement. + * CACTRL=1: Root control cache. + * TOP=0: Trap on Previlege. + * TOE=0: Trap on Exception. + * TIT=0: Trap on Timer. + */ + if (env & CSR_GCFG_GCIP_ALL) + gcfg |= CSR_GCFG_GCI_SECURE; + if (env & CSR_GCFG_MATC_ROOT) + gcfg |= CSR_GCFG_MATC_ROOT; + + gcfg |= CSR_GCFG_TIT; + write_csr_gcfg(gcfg); + + kvm_flush_tlb_all(); + + /* Enable using TGID */ + set_csr_gtlbc(CSR_GTLBC_USETGID); + kvm_debug("gtlbc:%llx gintc:%llx gstat:%llx gcfg:%llx", + read_csr_gtlbc(), read_csr_gintc(), + read_csr_gstat(), read_csr_gcfg()); + + return 0; +} + +void kvm_arch_hardware_disable(void) +{ + clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI); + write_csr_gcfg(0); + write_csr_gstat(0); + write_csr_gintc(0); + + /* Flush any remaining guest TLB entries */ + kvm_flush_tlb_all(); +} +#endif + static int kvm_loongarch_env_init(void) { struct kvm_context *context;