From patchwork Tue Mar 28 12:30:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 76044 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2182001vqo; Tue, 28 Mar 2023 05:37:37 -0700 (PDT) X-Google-Smtp-Source: AKy350YEINRIoXUZEpigdlKjQ9t02zhAtCBW2nEt2TDDEeZGaFjIzHlEkOL0H4x9bMw31H4622Ad X-Received: by 2002:a17:90b:4b10:b0:23d:41:3582 with SMTP id lx16-20020a17090b4b1000b0023d00413582mr15605571pjb.8.1680007057409; Tue, 28 Mar 2023 05:37:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680007057; cv=none; d=google.com; s=arc-20160816; b=kpvV5yDZmYqNeWQm2k3W4c/D2n3onZlx/YOWkp3um3bWdJLEIxeTZGwRIKoiFLKO/O 1UPJ4hQDc+VpeugJXcwkHTAuOtVHI81TYm4r8vKbkEfbYUAVWLFHDZu0aneHfZhbkw+f VOaA8KQuzOpscyHmFdUfjJyXcPyKgKS1LnnFR2D+1BestWzsmgRg9OjB09OBVfFoe6kw 3gAqfYZTTCmfbYzvbqA8V4CgchF9F+0mT965kmwLu+/uox3n1x7S4ILkp0nOALNU0D8T XDYKJsr9SwYvCSF0G9upkC3zR0950HBce5LvvwU0pSylHWvR+0diVhlAZeJXgzo8jqA4 JYoA== 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=HZAB9McFJg8Ho5mhdGc5VK0/qLtfMOKnLLogwf1yU6U=; b=oF80Z0JSH8VdxvNEARvnjLnEDNwMQNpQvhoQJPzy0j2bdwBOu+VzmlBjXZmJ/rKD5p 2nNZqu9NnyBlrlSG3mwuRiZV2N58hHzZQG6u7Iio8SWHd3EO5gHaOOO9cN/mE9GdaVu4 nNJ0QUGZRHrQuE2Clr7+8ugt4tEeR4VfqOm25djy9US0f5cBTh18lDOdrQUao7C6AP4Q TBkXXoSp+bzk8xKp1lNHzsMb/OBfqyjaeGAgKzrW9Bve4M8vhja7HJozAVajYFK0uJNq e6PGY+P9Sgs/rKehspZ7aViK8VH3AhoKnO2Nz+ZU29l/5yhIH7HK2DnqP/fxEa37jP2v up3g== 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 c10-20020a17090abf0a00b0023bdb8f6176si8021061pjs.128.2023.03.28.05.37.23; Tue, 28 Mar 2023 05:37:37 -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 S232475AbjC1Mbl (ORCPT + 99 others); Tue, 28 Mar 2023 08:31:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229778AbjC1Mbf (ORCPT ); Tue, 28 Mar 2023 08:31:35 -0400 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3168B9764; Tue, 28 Mar 2023 05:31:21 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8DxE0wZ3iJkvBgTAA--.29459S3; Tue, 28 Mar 2023 20:31:21 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8CxPL4X3iJku2cPAA--.12046S5; Tue, 28 Mar 2023 20:31:20 +0800 (CST) From: Tianrui Zhao To: Paolo Bonzini Cc: Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao , zhaotianrui@loongson.cn Subject: [PING PATCH v4 03/29] LoongArch: KVM: Implement kvm hardware enable, disable interface Date: Tue, 28 Mar 2023 20:30:53 +0800 Message-Id: <20230328123119.3649361-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230328123119.3649361-1-zhaotianrui@loongson.cn> References: <20230328123119.3649361-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8CxPL4X3iJku2cPAA--.12046S5 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoW7Ar4DJrWxKFW5ZrW8uF4kWFg_yoW8tF1kpr W7AFW5Ary5Jrnagas3J3Zxtr13GrWvgay7Ja12ya45Jw4j9F4rXF95Kr9rJFy5X3y8XF1S v39akFyF9F1DArUanT9S1TB71UUUUj7qnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU bcxFc2x0x2IEx4CE42xK8VAvwI8IcIk0rVWrJVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4 AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26F1j6w1UM28EF7xvwVC0I7IYx2IY6xkF 7I0E14v26F4j6r4UJwA2z4x0Y4vEx4A2jsIE14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7 CjxVAFwI0_Cr1j6rxdM2kKe7AKxVWUAVWUtwAS0I0E0xvYzxvE52x082IY62kv0487Mc80 4VCY07AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VCjz48v1sIEY2 0_WwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2AFwI0_JF0_ Jw1l42xK82IYc2Ij64vIr41l42xK82IY6x8ErcxFaVAv8VWrMxC20s026xCaFVCjc4AY6r 1j6r4UMxCIbckI1I0E14v26r126r1DMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xv wVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjx v20xvE14v26ryj6F1UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20E Y4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267 AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7xRiTKZJUUUUU== X-Spam-Status: No, score=-0.0 required=5.0 tests=SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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?1761615080057652273?= X-GMAIL-MSGID: =?utf-8?q?1761615080057652273?= 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 | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c index 2c6b4760f..d501c2e64 100644 --- a/arch/loongarch/kvm/main.c +++ b/arch/loongarch/kvm/main.c @@ -109,6 +109,69 @@ static void _kvm_init_gcsr_flag(void) set_gcsr_sw_flag(LOONGARCH_CSR_TINTCLR); } +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 gcfg = 0; + + /* 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 (cpu_has_gcip_all) + gcfg |= CSR_GCFG_GCI_SECURE; + if (cpu_has_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;