Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp6064551rwd; Mon, 19 Jun 2023 01:35:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4r92AswozSHQ9LNm9rhPOwGw51BeBzFNu8OyeMqE6N1f/k/+WKH0ZPRNtPO2xfPY2ifBRO X-Received: by 2002:a05:6a20:7d84:b0:100:6863:8be7 with SMTP id v4-20020a056a207d8400b0010068638be7mr6000894pzj.62.1687163706952; Mon, 19 Jun 2023 01:35:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687163706; cv=none; d=google.com; s=arc-20160816; b=xqw05pGy4p3hLyLRLA3fydbVbr6UPzpYzoEp/B2GEk9Lx75t1HP2i+I1LA3bnZ4/dH 3UZ21Q6dy1N4AqVHmsID7TX2U9IwY2g1hoEHq3Pcc/XRvBhTBMWWzVHFg3CjVCa1jo5p Q14p5YB1XAIH1xB7GThICYte93nB4r6Gpao0KF9rdwXnfSTfg5PaquamuwS7YLf7hxKH S/TZjU5JJUQmH0rNrgduXVMKrzbmI96RVB7iK9IzlwJ0nwI1tzxNSMomVweywZMlCjoY n0Vv2HoYeK0L4TTf6d9lS0qezt7nkacKVXjftMzdXtNFdU4jZc1iVnqi8io/xSyuFag3 LdZQ== 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=2YKns34UztuA/F4HKz/tmS8Z+uFHiWd85323lpi0N7k=; b=uLlwGPDzK17QKLZyUIfZ0SKNhRz9T4oxVKzbncxNw6eSR1DHneEo3vaAtdra2YJZ1F 2+m6paneCQI9r+D59/kRVoLP2fqANxeqmW4XaMU/yD9UnfaMINo60m40AevolCbc3Rgf iuPGKcsWxmuS9Qp6n+bpOqPtn3/DBYHP7p7GjrriGhHAfSli7/IWjpE6EfdI5FK3ER7l gzCEUYvGzXr/g/kGOZFjBK9g1xZ3uWHXkNbnFbo2kxFkROgWWKT4IzZyh4KWb9MTW/w4 hGjX2g3QE8ujtAalas7sjyNXwSqPs6l3wjyQgMELhqjvEVR3LlFd9ifVn+JtQ5hKbJR1 COuQ== 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 n22-20020a637216000000b00553a587e5dasi3267833pgc.833.2023.06.19.01.34.54; Mon, 19 Jun 2023 01:35:06 -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 S230421AbjFSIde (ORCPT + 99 others); Mon, 19 Jun 2023 04:33:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230328AbjFSIdF (ORCPT ); Mon, 19 Jun 2023 04:33:05 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E4A49E70; Mon, 19 Jun 2023 01:33:03 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8DxTuu8EpBkN8sGAA--.13933S3; Mon, 19 Jun 2023 16:33:00 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxC8q3EpBkAgsgAA--.15169S5; Mon, 19 Jun 2023 16:32:59 +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 v14 03/30] LoongArch: KVM: Implement kvm hardware enable, disable interface Date: Mon, 19 Jun 2023 16:32:28 +0800 Message-Id: <20230619083255.3841777-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230619083255.3841777-1-zhaotianrui@loongson.cn> References: <20230619083255.3841777-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8BxC8q3EpBkAgsgAA--.15169S5 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 f98c1619725f..b81b90e0c59f 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:%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(); +} + static int kvm_loongarch_env_init(void) { struct kvm_context *context; -- 2.39.1