From patchwork Mon Mar 10 15:12:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 14010363 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3C511C282EC for ; Mon, 10 Mar 2025 15:47:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=YvHI0Xsq8V4lH0iYdntivJhibhPUogzsdbm0bI8CDoE=; b=4MEhrY144rtPZG /nutETQ4xdyPNdMMeCv7iGdFd7izyjJLA+0rqmwbTmeTHryFh5uqo/y2BRWxZgxOt4/BmJwwdmLxr wU1MgAgWU3DOw00EB8TpwTY3djX/EQ/wCUrG4Y/S+GQB6KQLLGAW0KKNANdJ2evZ5gGNBPDqcKEQc apYe16/oWC07hpPdTQAg4UZrLuVVrXBZftAN4eM/OfmYsE+AYdh0Ah4MWJ+PhYptoY29ToVqk3WA1 gnNrA3ZJE1PuY+VK5UlR11M2h/UsamzB+NdE59JPwk33dn49yXIydN+UK77PCTkBIK4HpGxoDIIYw 6ETQEDbpJZKxsCppH/lg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1trfM8-00000003GIo-04DT; Mon, 10 Mar 2025 15:47:52 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1treq5-0000000386h-2nFt for linux-riscv@lists.infradead.org; Mon, 10 Mar 2025 15:14:46 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-22580c9ee0aso13297765ad.2 for ; Mon, 10 Mar 2025 08:14:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741619685; x=1742224485; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YO198dOv/JV7u6RiN4qQUqM3nesMDxD5mvDVMd+k3NU=; b=oW3wvlfTa18cAbhHPv3vFnuw6Own1lv1iwH8S9iIe+1f52e161sGYXV+AoAlF6fC0b 6TiKVDt2MX/+K+2ZqzWfGVOSMOFvlcHgY5H/RarH5TLEj0j2b6eG37rLYaaCsCtnodDV zus9WGCUqpZWmtrojFNtRDA+P7MFyc0CAok0ySye3eAEIPMSs2T6XsZe4IDe8e5EBUm/ weemXkOgdjD+B1NiyFzyefreTFoghFTEp3BMxqLnTBcdqQRaLGQinzmUYnyYeVlekZMa MB3Aq4kduCPxbuk6nQOgJAk6mXJZ1bO2nBnZyp33b+DlS1bszMfwJc1yly7IXcNzbARt fNTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741619685; x=1742224485; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YO198dOv/JV7u6RiN4qQUqM3nesMDxD5mvDVMd+k3NU=; b=w34LgHgBFUFN4OG4AaB7meviAHv4do8Vi9f88r/WBydTLmlNSiuiMxj/9FwmP61BaD Rg6nvTwkumO/t5/oQJP5sCYoEJml7b41N3vIR/885I0X3luqu058n2854n7ccLkA2z8B ILdaKlqYyJV9a41pnHkBwUVPi9a1rVBHMG6bn3+3FtyDG8beXd6Z4kagrOBXlhFSF4Y1 hunetxFU2zrcN5l1E0KFVdKeIcAddbgZHgxs0Kgh+Nkcs+EkghumYh9+us5UWW8ECROf EgUcaMcs26qNesI53KJyY5Kh8Zof/Lx3ddrZ8jyIsbP8OMBApqmrhyTukAfnQTdnXmzq G0XQ== X-Forwarded-Encrypted: i=1; AJvYcCW01qZWgh4XxCGIUlNthlEWOljA6oMNUsZxeMY/snF6WXJb1evu1td3Sr3A3E7FZ++oNvZrsv7KSvhjkQ==@lists.infradead.org X-Gm-Message-State: AOJu0YworANcWT0pyJbKVDYCPXhUTk1eaKAmqNuiqjMWT/CErN86oeSS syxFSXlaJn8+ckY8m+ubXc/DCUd7jFtkUF3g7lXtHJeWc4ndGBSG6G+XVdNo8VQ= X-Gm-Gg: ASbGnctrU+0bP+9TbMsNzxWRlCl2j1C2EAu83AavjOGgw//QgDKdBaQdpNXvI4Xw49s Iz6u3YjIfElBjLSrBTG181L0cTXlzbGpXZIgm8aF+MV+h5ncuaRCb7kjZQyxNGqwAecF7Wywv48 cAaHB5FpVy2cnxWeqCnrvjhPhA3GSbhYd/XfBmO8Pm6NsACiVCFvY6lGbEsswXxnwGsgpKQRy1b 6QRUWEqQbyxJMI7VRpFesa9MoG9y60WZqdkJnSoSzZZHQkshZlDi+VozTNt5Tc29AaXzBdi7RL3 Y2FUrENfzxrBSY7lU5bnHT0hMOr1er7krZjNVCIPktg+cg== X-Google-Smtp-Source: AGHT+IGIS2XxIf4TQa/glKSShb2DPMKV2nM+32P/jFh4qY9T5snUhs8ena+pzkKO/eEMPCMRH58+YA== X-Received: by 2002:a17:902:eb81:b0:223:5ca1:3b0b with SMTP id d9443c01a7336-22428bd592amr282084395ad.40.1741619685003; Mon, 10 Mar 2025 08:14:45 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-224109e99dfsm79230515ad.91.2025.03.10.08.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 08:14:44 -0700 (PDT) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: Paul Walmsley , Palmer Dabbelt , Anup Patel , Atish Patra , Shuah Khan , Jonathan Corbet , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Samuel Holland Subject: [PATCH v3 14/17] RISC-V: KVM: add SBI extension init()/deinit() functions Date: Mon, 10 Mar 2025 16:12:21 +0100 Message-ID: <20250310151229.2365992-15-cleger@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250310151229.2365992-1-cleger@rivosinc.com> References: <20250310151229.2365992-1-cleger@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250310_081445_738902_AB25309E X-CRM114-Status: GOOD ( 14.60 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The FWFT SBI extension will need to dynamically allocate memory and do init time specific initialization. Add an init/deinit callbacks that allows to do so. Signed-off-by: Clément Léger --- arch/riscv/include/asm/kvm_vcpu_sbi.h | 9 +++++++++ arch/riscv/kvm/vcpu.c | 2 ++ arch/riscv/kvm/vcpu_sbi.c | 29 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h b/arch/riscv/include/asm/kvm_vcpu_sbi.h index 4ed6203cdd30..bcb90757b149 100644 --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h @@ -49,6 +49,14 @@ struct kvm_vcpu_sbi_extension { /* Extension specific probe function */ unsigned long (*probe)(struct kvm_vcpu *vcpu); + + /* + * Init/deinit function called once during VCPU init/destroy. These + * might be use if the SBI extensions need to allocate or do specific + * init time only configuration. + */ + int (*init)(struct kvm_vcpu *vcpu); + void (*deinit)(struct kvm_vcpu *vcpu); }; void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run); @@ -69,6 +77,7 @@ const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext( bool riscv_vcpu_supports_sbi_ext(struct kvm_vcpu *vcpu, int idx); int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run); void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_sbi_deinit(struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_get_reg_sbi_sta(struct kvm_vcpu *vcpu, unsigned long reg_num, unsigned long *reg_val); diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 60d684c76c58..877bcc85c067 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -185,6 +185,8 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) { + kvm_riscv_vcpu_sbi_deinit(vcpu); + /* Cleanup VCPU AIA context */ kvm_riscv_vcpu_aia_deinit(vcpu); diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index d1c83a77735e..858ddefd7e7f 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -505,8 +505,37 @@ void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu) continue; } + if (!ext->default_disabled && ext->init && + ext->init(vcpu) != 0) { + scontext->ext_status[idx] = KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE; + continue; + } + scontext->ext_status[idx] = ext->default_disabled ? KVM_RISCV_SBI_EXT_STATUS_DISABLED : KVM_RISCV_SBI_EXT_STATUS_ENABLED; } } + +void kvm_riscv_vcpu_sbi_deinit(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context; + const struct kvm_riscv_sbi_extension_entry *entry; + const struct kvm_vcpu_sbi_extension *ext; + int idx, i; + + for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) { + entry = &sbi_ext[i]; + ext = entry->ext_ptr; + idx = entry->ext_idx; + + if (idx < 0 || idx >= ARRAY_SIZE(scontext->ext_status)) + continue; + + if (scontext->ext_status[idx] == KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE || + !ext->deinit) + continue; + + ext->deinit(vcpu); + } +}