Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1815556rwd; Fri, 9 Jun 2023 02:33:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4PPOK4kSIoKBOsHh8WuJlS+LRD9Qk8K/5hbuVsPkDY1BLkad31yZXM5b53opMp/lwYZNb3 X-Received: by 2002:a05:6359:3a1:b0:129:d026:e9f2 with SMTP id eg33-20020a05635903a100b00129d026e9f2mr861561rwb.11.1686303185616; Fri, 09 Jun 2023 02:33:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686303185; cv=none; d=google.com; s=arc-20160816; b=APxDhDuKNZ/WeCCr7VmxL1VzjaTWOsQsF5AbxO50Ayw2l+py5XD2bxiyPLB2A3c5gl 9dxSztRu5pAe1JztFDMW7yZIifeSSuq4HJTDbBrnc55D6bNEaxcmf8iyqr8a8aJH2he4 PQ1f4yHpC8l9+hl9Lp2swFBHNoyj3ZpohMM/czZ790xNHqmWKbMxyt+gU+2qeKe6PBZM RDPHGRJ44DqWT0i9I3NKbi+4NNLZ0C5spFT3oePNwLnS71WEUJDtZqfrgGZOOyAw3sbf 5NAIA0NLCceTYTqavrh4f1bYE+XRrFbg0JZCapuxymkTTvIpCS2GrjV13AxyyJPDG1Gk ipGQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u123-20020a637981000000b00524eef20da6si2401771pgc.642.2023.06.09.02.32.51; Fri, 09 Jun 2023 02:33: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 S230388AbjFIJO0 (ORCPT + 99 others); Fri, 9 Jun 2023 05:14:26 -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 Content-Transfer-Encoding: 8bit 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 Implement kvm hardware enable, disable interface, setting the guest config register to enable virtualization features when called the interface. Signed-off-by: Tianrui Zhao --- 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; -- 2.39.1