Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1801671rwd; Fri, 9 Jun 2023 02:17:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5lD6mR9eQ3p0rrUqQ8Lcn0/0dY+CFvPbOnH5BDcIQIq8w7A6vF7TiTcet+mkTOM1U0nTdB X-Received: by 2002:a67:efd4:0:b0:43b:131f:1b50 with SMTP id s20-20020a67efd4000000b0043b131f1b50mr627199vsp.24.1686302247663; Fri, 09 Jun 2023 02:17:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686302247; cv=none; d=google.com; s=arc-20160816; b=JDKkZnukNIvemAJp6nLGmp+1AQy0T4K51TTVCB7jj+nplfkgm9ydJIaNetVX7QeQ5d nObddB+lFCWteqA05x6EStQN4xj+Lvt+i9p1XUmeKJ04lWgvdScFyxDhcbFXOE8uDIwz cMO9/I8lQ5mSDC+JFju3PyQ2Y8Tf6CiGWg1YmZW+mVu/tOBUFU0Rn3XFSKQQ6mbdstDC MQYuwlAxQswpkQMZd38wC3erAtDNKGzLQJmneCeBLFnxMa+xEbo/B3wvH73gVBF22wKK W6dO1Y5YUtCAWPEvF4OBaJMqAloewso2DM3HL5JSooQwtLDMXAdaD0g47Ng9H/bYUJOt 68hQ== 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=mcFfYGsFpwzKFo/xoBlN6j8RGGXo6ZePzDtKqKhbq4vmcJNpAM1lb6k0KD4WCyfuTu +qsuJ7Oaa8jQiZRrkhLALrGNlRqV9lsKM5HykUWyRzCQolLRN1fXi1srCfRn4owlcM/o Kyodh25btRidC4H1U2cswTRFMJVh9z6ATWpLGFo6k/OUhsPnGuk7zsVfrSIG3zkJ+Az3 LmAkHCrmBCmiunD50P9DElaqJuHfJx9kc351KiBAlRiCnmRoiqNB+khwbPIEJXsrVfKa wVeul+MwHq3FOmNXusNGatLekYque0oH46XOnhllEIwveypa/BSF9sN28Lu7+O18S0sX /yXQ== 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 t75-20020a63784e000000b005440dd16740si2440709pgc.300.2023.06.09.02.17.15; Fri, 09 Jun 2023 02:17:27 -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 S241079AbjFIJGg (ORCPT + 99 others); Fri, 9 Jun 2023 05:06:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241087AbjFIJFu (ORCPT ); Fri, 9 Jun 2023 05:05:50 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A4979173A; Fri, 9 Jun 2023 02:05:47 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Cxe+pp64JksfsAAA--.3014S3; Fri, 09 Jun 2023 17:05:45 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxluRO64JkSXEKAA--.31854S3; Fri, 09 Jun 2023 17:05: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, tangyouling@loongson.cn Subject: [PATCH v13 03/30] LoongArch: KVM: Implement kvm hardware enable, disable interface Date: Fri, 9 Jun 2023 17:04:51 +0800 Message-Id: <20230609090518.2130926-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230609090518.2130926-1-zhaotianrui@loongson.cn> References: <20230609090518.2130926-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8DxluRO64JkSXEKAA--.31854S3 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