Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp1670189rwo; Wed, 2 Aug 2023 19:28:06 -0700 (PDT) X-Google-Smtp-Source: APBJJlFiC+Qt1Gl8eRuQmZvoR5DLeRxamJ3s0OH2M5SM0raSoYf1sYWR6PxYU0D8O6KnTy8Gwg8b X-Received: by 2002:a17:907:774d:b0:991:d9cb:247d with SMTP id kx13-20020a170907774d00b00991d9cb247dmr6184474ejc.57.1691029685781; Wed, 02 Aug 2023 19:28:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691029685; cv=none; d=google.com; s=arc-20160816; b=NwyAeQ9oWfySGvaTDUcYAuQnFU79GEBmvldJdm5ETGtbDkYmMTNQxQBRn/Ib0fb18D EiC/LmI5+9pItuOLwjDZkaF+TuMV35Ik3tLnALkgtH5UijX/MyZkULxcuTDCCqJXhXJy 4t+OJ+HNSyqLB8suJruA+pVPUillOPLiVQWthW9NpvUGsZNkajQ1L4MBooC/pRT12foc Whv0IHdp5bwDLD7vedDQHX8gf6n+/gfqpdgq9LsErYC3X1telAygrJbZbQtdWXIOrKRo LjFWyvUqQYeCAL9gqgikws6+T107INhd2CXqqBMmdi4/jO/g46t5BEf0TRnMcFgkq+CQ VEPQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q22-20020a17090622d600b0099304c10fd0si12145926eja.991.2023.08.02.19.27.41; Wed, 02 Aug 2023 19:28:05 -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 S232429AbjHCCWG (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 Content-Transfer-Encoding: 8bit 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 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; -- 2.39.1