From patchwork Fri Jun 9 09:04:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 105452 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp810754vqr; Fri, 9 Jun 2023 02:08:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4De+yFUcSUzqeWhazed07yfqDFWsk9prioytHqZvS7jhTskyLAQ2BqXGSifj6qD4Fy0DF2 X-Received: by 2002:a05:6358:a55:b0:129:bf30:3ee with SMTP id 21-20020a0563580a5500b00129bf3003eemr708748rwb.7.1686301723512; Fri, 09 Jun 2023 02:08:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686301723; cv=none; d=google.com; s=arc-20160816; b=hzF4CvWvzBXRbq4idqj3cFmiTQ/dvXniouOhCvQ9cDzD/kA81wOm0bAyL7ezmfI8D5 aEtL35j705RX9361A7WnoVd5TuRbXii2CJxoh71Ush/Sz7VWNYGhr66sz/GcJAUzrC3p TM5Ee62Dtj3yOmlDA1y81pOnOyVnuS+0eTE4LQl4JH5NfgEZkx8kZR/G2AGhzP+NsIyh xbRP2Cgp23O1VJV2BHE8pEeAc6VvXJ4m4eJc3/CiVG9sujklLEPAAK6YKAymSl6uMt8j GsldcA4im/OeTACp1I/yGqXEpifW/aOY6Kj0tTJjOKYuRLBjZPfhAssyGaYXXBavNv7a WSFg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b19-20020a639313000000b0052872c32995si2278933pge.724.2023.06.09.02.08.28; Fri, 09 Jun 2023 02:08:43 -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 S241097AbjFIJGg (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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768215516134369961?= X-GMAIL-MSGID: =?utf-8?q?1768215516134369961?= 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;