From patchwork Mon May 15 02:14:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 93814 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp6618479vqo; Sun, 14 May 2023 19:39:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5b0b7sdlEtPX4rKRteZIyufsjWHbEM+TuivCAOi9+zEG6G5XO5iBzuVN8yDbXfLXqdLc5V X-Received: by 2002:a05:6a20:2452:b0:104:f9d4:141c with SMTP id t18-20020a056a20245200b00104f9d4141cmr8098898pzc.19.1684118386704; Sun, 14 May 2023 19:39:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684118386; cv=none; d=google.com; s=arc-20160816; b=mrjdIJDtzMQqLPI8SesRT4KB85MpQ67M5wfiqQhcXqENwSPNOUMvmfOFryHdlyPL/S pCHUiqlH8nuDDjKa4UGMuXNnIVMP2qml02voIY9MShtJsKCyPS6MAu0uP5yfMaBqKz6J IV3kje8qOlJwA1fvGg8MDlUwZF9DaJ+bo41NNmgM1R6XbAsUG8pfneTkgJtq/7Sr6jOs AF5P6ojHEycKWupMGADu9gsfNQhhPz8eHlt6EzMURqW7++DLCWwgLoAyA7nV2yg5RrJA sylZFkiKWFjEpidUcdjaoyCyphha18JtmRq3CxQH+kzZBmSi6Ges/9fgiPt3PDRTATua +tTQ== 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=XcUFcQljpSqIiV6U/idVV39kW9o+pKmxjG+UjwiBWYQ=; b=cMLGu2i5eUEGfHkEJ+FHWY9PzNrg+s0vzP8ngUqB5oOlZTFV4gDGSXQeXOufmg6i+c Tif0QP/HggXww0AOyZDDA75TF2RVUnzTsCL+1VmUaX+gaw2QTwNJtbCMVvPEO5v97NSt XQLBTeTwifgby9k5BjgiV9ao09NdEuFDDfCr458XAwF+sL1WBvZLAmt3l+N7mxHqvAis 5iQL2BcRgUKXgh3LnNKtNZ68pfbd3WTyo3gPT7ux6ws8JBTf8YB9/fMKJ3OhWvyKaWeN Ek0IbISNqcsiYyMdEMWeiZCeDkw3ia7XP+k65Fv8iHhYDpwfGO681pMWFPuiDER492Y5 fXdQ== 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 x24-20020a63db58000000b0050beb3fe627si15412191pgi.459.2023.05.14.19.39.34; Sun, 14 May 2023 19:39:46 -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 S238322AbjEOCQJ (ORCPT + 99 others); Sun, 14 May 2023 22:16:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234226AbjEOCPc (ORCPT ); Sun, 14 May 2023 22:15:32 -0400 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2AC5E1712; Sun, 14 May 2023 19:15:27 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Cxe+q9lWFkR68IAA--.14800S3; Mon, 15 May 2023 10:15:25 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Dxn7O6lWFk0KJeAA--.33838S5; Mon, 15 May 2023 10:15:24 +0800 (CST) From: Tianrui Zhao To: Paolo Bonzini , Huacai Chen Cc: 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: [PATCH v10 03/30] LoongArch: KVM: Implement kvm hardware enable, disable interface Date: Mon, 15 May 2023 10:14:55 +0800 Message-Id: <20230515021522.2445551-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230515021522.2445551-1-zhaotianrui@loongson.cn> References: <20230515021522.2445551-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Dxn7O6lWFk0KJeAA--.33838S5 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoW7Ar4DJrWxKFW5ZFyDAF4kCrg_yoW8tF4fpr W7AFW5Ary5tr1SgF93J3Zxtr13GrWvgay7Za12ya45Xw4j9F4rXF95Kr9rJFy5W3y8XF1S v39ayFyF9F1DAwUanT9S1TB71UUUUjUqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU bwxFc2x0x2IEx4CE42xK8VAvwI8IcIk0rVWrJVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4 AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26F1j6w1UM28EF7xvwVC0I7IYx2IY6xkF 7I0E14v26F4j6r4UJwA2z4x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6x kF7I0E14v26r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l 57IF6xkI12xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x8ErcxFaV Av8VWrMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCF04k20xvY0x0EwIxG rwCF04k20xvE74AGY7Cv6cx26rWl4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI 0_Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF 1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Xr0_Ar1lIx AIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI 42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWI evJa73UjIFyTuYvj4RKtC7UUUUU X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, 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?1765926121575108983?= X-GMAIL-MSGID: =?utf-8?q?1765926121575108983?= 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 228d21dc40ae..08e8ec497660 100644 --- a/arch/loongarch/kvm/main.c +++ b/arch/loongarch/kvm/main.c @@ -188,6 +188,69 @@ 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 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;