Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4284817rdb; Thu, 14 Sep 2023 18:54:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGg8Qnlv4Ofug1R0kXaqSXpj8ycAKV86RxXVyPu6AY7i3xabhmhCTSODmsaEpsG9PNWGoYe X-Received: by 2002:a05:6a20:cea8:b0:14b:f9b2:eb7 with SMTP id if40-20020a056a20cea800b0014bf9b20eb7mr418919pzb.59.1694742861210; Thu, 14 Sep 2023 18:54:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694742861; cv=none; d=google.com; s=arc-20160816; b=hZvnoKjcOop01nPwI3kjVdxzu8DvLOa969oNxMbNeF4ht/0FZcBChXBYaPHbyXd3XG +Z2y3mED6FuBaLKsWSZ02CDBdr3yJjX50VpzeWNOtrTHt9vAsuwm/iimGBOsWtlB6AB6 Ie8B1yde6mVn/L7sZ1Uwe1E60TBJzjb53xOif+nEzmqDg/EMtkL+DdihmPbH1FR+DFvg Pf6lXJiGy/U75hej5q8GUrk7UDjB5nsyt+npbwcHx+BYjhF+PZlwim7E67o1uSK9bD5D 3J729452FaERCXs2Q2Bb+ljiC+NQzep/+DOkrwiQ1DQhMZzw3dJjN0/1y0b+qIEDyvhw oZ5A== 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=ZcjHZIN21wr6ERNwDZiEy0ikkOdS2ZZzi2okhm0UjBU=; fh=ejoaVAUUbBXsA/6jM/beuI9xCYtV2osdR9SpI7hjY84=; b=iwYljFFbURHom2J1tjbww/FZPFz9s+rdE7diHM9xpUZuyJGGFX41FWhuvWjHTXjj3T gDdGa2JgAa9bK6kG563mUYj8FBUNAqaFn+hI/eHpV34CXLg4KyrGQcxqNLklMC2wBW4w GqpedewibR1mDmTzojAohUEpq0Ut6pKoLD9i2QvRiEzXfBQT5LdBS0xegCTwzHzp100W LkqFOCIDwmCfvXUr5yjI4Wywyzqi1z9TWODEs9/XBcXgqtaPFmFSB4zOM7iojejfBjN0 yx5vELIDqhISQ9HuWDesRpBmebHdxOGDJOV1s6dfWa+Tne4iXLOXY6JtdfmFw2nv+Ivv x3+g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id o184-20020a62cdc1000000b0068e45c6ca3fsi2406795pfg.93.2023.09.14.18.54.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 18:54:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id B70DD83B6BE7; Thu, 14 Sep 2023 18:50:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231411AbjIOBuJ (ORCPT + 99 others); Thu, 14 Sep 2023 21:50:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231322AbjIOBuC (ORCPT ); Thu, 14 Sep 2023 21:50:02 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 52B0B270A; Thu, 14 Sep 2023 18:49:57 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8BxXetDuANlif4nAA--.6266S3; Fri, 15 Sep 2023 09:49:55 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Axndw9uANl+ioGAA--.11927S5; Fri, 15 Sep 2023 09:49:54 +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 Subject: [PATCH v21 03/29] LoongArch: KVM: Implement kvm hardware enable, disable interface Date: Fri, 15 Sep 2023 09:49:23 +0800 Message-Id: <20230915014949.1222777-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230915014949.1222777-1-zhaotianrui@loongson.cn> References: <20230915014949.1222777-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Axndw9uANl+ioGAA--.11927S5 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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 (howler.vger.email [0.0.0.0]); Thu, 14 Sep 2023 18:50:13 -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 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 0deb9273d8..fdbea0bc65 100644 --- a/arch/loongarch/kvm/main.c +++ b/arch/loongarch/kvm/main.c @@ -255,6 +255,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 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:%lx gintc:%lx gstat:%lx gcfg:%lx", + 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