From patchwork Thu Aug 3 02:21:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 130283 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp872894vqx; Wed, 2 Aug 2023 19:59:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlE3Ln+DJw9ds8R+Lh/mZ36QgTFTXEuV9J5eOD2L4uCzgvvbT/NtNxj+BVIvNpq5SZSYcp0Q X-Received: by 2002:a05:6a00:2814:b0:66f:3fc5:6167 with SMTP id bl20-20020a056a00281400b0066f3fc56167mr15248089pfb.1.1691031585001; Wed, 02 Aug 2023 19:59:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691031584; cv=none; d=google.com; s=arc-20160816; b=ZfsHyiG8SOZc9e/7Iz03Q9R/M+7u7p8uwqa5sHqmvIRPng9GE33UU2xOQsi2V0RM5h 30hKybDQ2w6DeC0RqQBlHiw9yHYC/fS5Wxo+GsnLhv58+Hy2QlbGBHfAuXwUNjx9APp8 /ZkLMtdSEMp1Egevn2yLNfFBbUmYGucVBDRk0F/bJ0inAr35LNCdpa3ksfHbF8bPAYQy SyHxliSyN3bLRoByOvmh2odRSySsnf67IX2dcFPVF6JODaZaU2+BaY08rZ3WGvMrIBmL Igz4quxnii7sVeu+VBm+XW2ViFEw06gONlgvFooTJgdyLnV20Qz4ByTPN1DbTBrzdVn/ YeyA== 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=hK4LHuooS2V6uI13NLKhDvzCkWdzSBw0nJppfDbfaXE=; fh=vBXozhnPXwSB3DJhjnKWz3f6luaN/KxHDpM6/QeZg4o=; b=Afe8CfP3Jubp9taffRcg0pAmk+qG0udU9p/1DyLk/TwfKu6BRIH2zBtRqUshnFdnc0 dVBEjTCDhMnGIix3kekqGpg5+HmFBXxoTfdXwoOFx+ccanBkcAMojMsf89NMSRtYciKW 9OvVzvc5iuOJkL16z8R94+uw/ZyMbuRg7AkPDrp2er887iTQcl8KwYmBUmb2x0plfFKq Clxj6kRKVxS4Wkxe/YysbpeqKNFt/pycP4/6DhBZ8jrUnDRyVWRc41vZd4fzprq2z68E z+lrWevl6QLYzBc9kjMss6ISVnmy0fbhEVN50qbNcnFS6RU8EdGo6FfHxpmMku0efGep TFog== 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 t23-20020a656097000000b00564686957f6si4094678pgu.826.2023.08.02.19.59.31; Wed, 02 Aug 2023 19:59:44 -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 S232445AbjHCCWG (ORCPT + 99 others); Wed, 2 Aug 2023 22:22:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232109AbjHCCVu (ORCPT ); Wed, 2 Aug 2023 22:21:50 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AE61C128; Wed, 2 Aug 2023 19:21:48 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8BxXes6D8tkp2gPAA--.30673S3; Thu, 03 Aug 2023 10:21:46 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Cx_c4yD8tk8oNGAA--.51268S5; Thu, 03 Aug 2023 10:21:45 +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 v18 03/30] LoongArch: KVM: Implement kvm hardware enable, disable interface Date: Thu, 3 Aug 2023 10:21:11 +0800 Message-Id: <20230803022138.2736430-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230803022138.2736430-1-zhaotianrui@loongson.cn> References: <20230803022138.2736430-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Cx_c4yD8tk8oNGAA--.51268S5 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: INBOX X-GMAIL-THRID: 1773175135247150450 X-GMAIL-MSGID: 1773175135247150450 Implement kvm hardware enable, disable interface, setting the guest config register to enable virtualization features when called the interface. Reviewed-by: Bibo Mao Signed-off-by: Tianrui Zhao --- arch/loongarch/kvm/main.c | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c index c204853b8c44..46a042735d34 100644 --- a/arch/loongarch/kvm/main.c +++ b/arch/loongarch/kvm/main.c @@ -195,6 +195,68 @@ 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; +} + +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:%lx gintc:%lx gstat:%lx gcfg:%lx", + 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(); +} + static int kvm_loongarch_env_init(void) { struct kvm_context *context;