Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2333638rdh; Tue, 26 Sep 2023 22:15:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHTPTftUtKdD2ftqFRraUUlDeCtYxG9fG4bj5BojUx302mq1Qq+bP/2bhg1N5kSqeV2jw9z X-Received: by 2002:a17:90a:d70e:b0:277:7810:ac74 with SMTP id y14-20020a17090ad70e00b002777810ac74mr1830254pju.10.1695791715722; Tue, 26 Sep 2023 22:15:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695791715; cv=none; d=google.com; s=arc-20160816; b=nXm+aGOpwLgv+gGO1YoAXMECCfCPn81qTOLp0zt0RaMLJVMsYwG2lwGOoMpocsGvbz vUmOAzFCxCDiBn4NpSWqo/Wsxp2Gauif/CYKI9S7fcfcOI82n6MImlNzR77DcQqLg5uH C3OzDcpx6rNPAKG50eLACGNJfcwQF0MMYisVxBy/wrGzrqlknUQsivt8N1Kn8mLAMwW9 WDuHoe5iy2oQMV4jXIFRaWLf6SqvxSK2luLLetDDKtTcVyGc4sCPOqfIKKMWgDnnkeLw gPTrny+Y+rVWrziaMnLcubsGBOidv/7k6O+8HSct9IkLyERtIAfYboNfeJyjEz4qsShv K8rg== 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=ckbTaXdahTeBmRVun7/4kBSpA2pZAvC6Ls+iHtlKXxw=; fh=DndB7O6DTG6Sgonrk2zkDJR5MPyE/Qek5uYuwsra5zw=; b=jVAIrstCKJb0FCWE+fLnB3kuJpC6wi81Sbi/V2FJetgLpogXJwuVIzgpu7CKjlgMAV cxQI+UM4GQ0PkYnx8rDpFayJTkGzJJyO5JTVu1HBd66GBMmZK4O5w8t+gb3X6rqCbTrC uVp+wliqvvglk7VPU2XFWeQBUfd8PRo3jGKz5AxL7lQABYXDocAR6mUM36OuTHo4hPBo WUkLzdYLOo/hLAScpTMAU+j0g+zD5dH8S85Lyhunsjdmz6+yrntJ2zxvX9xXgeiktB2F OkGjPTEak3lb1mR/7dXAJAjvE3oFYKh9fUzwmdfkn5mFytR6/a5sOkSeKGjsKawt8YNE pTYw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id x7-20020a17090a970700b00273ebc6a4dcsi15955728pjo.121.2023.09.26.22.15.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 22:15:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id EA9AD810678C; Tue, 26 Sep 2023 21:34:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229802AbjI0EeJ (ORCPT + 99 others); Wed, 27 Sep 2023 00:34:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229543AbjI0EdR (ORCPT ); Wed, 27 Sep 2023 00:33:17 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DD0A0121; Tue, 26 Sep 2023 20:10:04 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8CxLOsKnRNl3hYtAA--.15390S3; Wed, 27 Sep 2023 11:10:02 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Cxjd4InRNlhZETAA--.42466S5; Wed, 27 Sep 2023 11:10:02 +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, Huacai Chen Subject: [PATCH v22 03/25] LoongArch: KVM: Implement kvm hardware enable, disable interface Date: Wed, 27 Sep 2023 11:09:37 +0800 Message-Id: <20230927030959.3629941-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230927030959.3629941-1-zhaotianrui@loongson.cn> References: <20230927030959.3629941-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Cxjd4InRNlhZETAA--.42466S5 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, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 26 Sep 2023 21:34:18 -0700 (PDT) Implement kvm hardware enable, disable interface, setting the guest config register to enable virtualization features when called the interface. Reviewed-by: Bibo Mao Tested-by: Huacai Chen 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 267c0505ea..1c1d519950 100644 --- a/arch/loongarch/kvm/main.c +++ b/arch/loongarch/kvm/main.c @@ -249,6 +249,68 @@ void kvm_check_vpid(struct kvm_vcpu *vcpu) change_csr_gstat(vpid_mask << CSR_GSTAT_GID_SHIFT, vpid); } +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 gcfg, gstat, gintc, gtlbc. All guest use the same config */ + write_csr_gcfg(0); + write_csr_gstat(0); + write_csr_gintc(0); + clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI); + + /* + * 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("GCFG:%lx GSTAT:%lx GINTC:%lx GTLBC:%lx", + read_csr_gcfg(), read_csr_gstat(), read_csr_gintc(), read_csr_gtlbc()); + + return 0; +} + +void kvm_arch_hardware_disable(void) +{ + write_csr_gcfg(0); + write_csr_gstat(0); + write_csr_gintc(0); + clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI); + + /* Flush any remaining guest TLB entries */ + kvm_flush_tlb_all(); +} + static int kvm_loongarch_env_init(void) { int cpu, order; -- 2.39.3